【问题标题】:Ionic 2 Http POST Method ERRORIonic 2 Http POST 方法错误
【发布时间】:2016-03-23 11:13:13
【问题描述】:

嗨,我将使用 ionic 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

【问题讨论】:

  • 您收到的请求的当前状态代码是什么?订阅时只需第二次回调即可获得此提示...
  • 在您的订阅 .subscribe((res) =&gt; console.log("res", res), err =&gt; console.log('error', err)); 中添加错误处理程序以查看您遇到的错误。
  • 嗨..请查看我更新的问题

标签: json http angular ionic2


【解决方案1】:

看来你有跨域问题:

XMLHttpRequest 无法加载 http://url/login/。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'http://localhost:8100' 不允许访问。响应的 HTTP 状态代码为 403。

https://en.wikipedia.org/wiki/Cross-origin_resource_sharing

您需要通过使用正确的标头在服务器端允许您的客户端域

【讨论】:

    猜你喜欢
    • 2017-04-19
    • 1970-01-01
    • 2016-09-15
    • 2020-10-19
    • 2018-07-18
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    相关资源
    最近更新 更多