Ionic2 Angular2 HTTP错误

时间:2017-01-09 14:53:01

标签: angular2-http

当我执行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)

}

1 个答案:

答案 0 :(得分:0)

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