嗨我打算使用离子2来调用http post方法。但在响应中我总是得到错误。任何人都可以帮助我。
这是我的json类型
{
“username”:”user”,
"password":”pass"
}
这是我的代码
import {Page, NavController} from 'ionic-angular';
import {Http, Headers, RequestOptions} from 'angular2/http';
import 'rxjs/Rx';
@Page({
templateUrl: 'build/pages/home/home.html'
})
export class HomePage {
constructor(http: Http, nav: NavController) {
this.http = http;
this.makePostRequest();
}
makePostRequest() {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http.post('url',
JSON.stringify({ username: 'admin', password: 'a' }),
{ headers: headers })
.map((res: Response) => res.json())
.subscribe((res) => console.log("res", res));
}
}
错误日志
4 236980 log DEVICE READY FIRED AFTER, 1437, ms
5 237420 group EXCEPTION: [object Object]
6 237426 error EXCEPTION: [object Object]
7 237431 error STACKTRACE:
8 237433 error
9 237435 groupEnd
10 237525 error Uncaught #<Response>, http://192.168.1.13:8100/build/js/app.bundle.js, Line: 7834
我该如何解决这个问题。?
我添加了错误处理
这是登录浏览器
Failed to load resource: the server responded with a status of 403 (Forbidden)
(index):1 XMLHttpRequest cannot load http://url/login/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 403.
app.bundle.js:67773 error Response_body: ProgressEventbubbles: falsecancelBubble: falsecancelable: falsecurrentTarget: XMLHttpRequestonabort: undefinedonerror: undefinedonload: undefinedonloadend: undefinedonloadstart: undefinedonprogress: undefinedonreadystatechange: undefinedontimeout: undefinedreadyState: 0response: ""responseText: ""responseType: ""responseURL: ""responseXML: nullstatus: 0statusText: ""timeout: 0upload: XMLHttpRequestUploadonabort: nullonerror: nullonload: nullonloadend: nullonloadstart: nullonprogress: nullontimeout: null__proto__: XMLHttpRequestUploadconstructor: XMLHttpRequestUpload()arguments: nullcaller: nulllength: 0name: "XMLHttpRequestUpload"prototype: XMLHttpRequestUploadconstructor: XMLHttpRequestUpload()arguments: nullcaller: nulllength: 0name: "XMLHttpRequestUpload"prototype: XMLHttpRequestUpload__proto__: XMLHttpRequestEventTarget()<function scope>onabort: (...)onerror: (...)onload: (...)onloadend: (...)onloadstart: (...)onprogress: (...)ontimeout: (...)Symbol(Symbol.toStringTag): "XMLHttpRequestUpload"__proto__: XMLHttpRequestEventTarget__proto__: XMLHttpRequestEventTarget()<function scope>onabort: (...)onerror: (...)onload: (...)onloadend: (...)onloadstart: (...)onprogress: (...)ontimeout: (...)Symbol(Symbol.toStringTag): "XMLHttpRequestUpload"__proto__: XMLHttpRequestEventTargetwithCredentials: false__proto__: XMLHttpRequestdefaultPrevented: falseeventPhase: 0isTrusted: trueisTrusted: truelengthComputable: falseloaded: 0path: Array[0]returnValue: truesrcElement: XMLHttpRequesttarget: XMLHttpRequesttimeStamp: 2726.8050000000003total: 0type: "error"__proto__: ProgressEventheaders: Headers_headersMap: Mapsize: (...)__proto__: Map<entries>[0]No Entries__proto__: Objectstatus: 200statusText: "Ok"type: 3url: null__proto__: ObjectarrayBuffer: ()blob: ()constructor: Response(responseOptions)arguments: [Exception: TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.
at Function.remoteFunction (<anonymous>:3:14)]caller: (...)length: 1name: "Response"prototype: Object__proto__: ()<function scope>json: ()arguments: (...)caller: (...)length: 0name: ""prototype: Object__proto__: ()<function scope>text: ()__proto__: Object
答案 0 :(得分:1)
似乎你有一个跨源问题:
XMLHttpRequest无法加载http://url/login/。对预检请求的响应没有通过访问控制检查:否&#39;访问控制 - 允许 - 来源&#39;标头出现在请求的资源上。起源&#39; http://localhost:8100&#39;因此不允许访问。响应的HTTP状态代码为403。
https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
您需要使用正确的标头
在服务器端允许您的客户端域