【问题标题】:Ionic2 Angular2 HTTP errorIonic2 Angular2 HTTP 错误
【发布时间】:2017-05-23 20:52:18
【问题描述】:

当我执行 POST、PUT 或 DELETE 请求时,我会跟踪两个 http 调用 第一个请求方法:OPTIONS,第二个是我的请求方法(状态代码:500) PS:我对 GET 方法没有问题。 谢谢

 @Injectable()
export class UtilisateurService {
  url:string;
  _http:Http;
  headers:Headers;
  options:RequestOptions;

  constructor(public http: Http) {
    this._http=http;
      this.url='http://localhost:8080/todo/user/';
      this.headers = new Headers();
    this.headers.append('Content-Type', 'application/json');
    this.headers.append('Access-Control-Allow-Origin', '*');
    this.headers.append('Access-Control-Allow-Methods', 'POST, GET, DELETE, PUT');
    this.headers.append('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding, X-Auth-Token, content-type');
    this.options = new RequestOptions({ headers: this.headers })
  }

 addUser(user:Object): Observable<Response> {
let data = JSON.stringify(user, null, 2)
 console.log('adduser '+data)
 return this._http.post(this.url+'add', data,this.options).map(res =>  <Object> res.json())
   .do(data => console.log(data))
   .catch(this.handleError)

}

【问题讨论】:

    标签: angular2-http


    【解决方案1】:

    检查您的服务器日志中是否存在 500 错误,这很可能是您的后端代码中的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 2016-03-31
      • 2017-04-08
      • 1970-01-01
      • 2016-12-15
      • 2017-05-01
      • 2016-08-06
      相关资源
      最近更新 更多