Angular2 Http PUT给出了错误的RequestMethod

时间:2016-08-10 10:20:09

标签: angular

我无法理解为什么我不能在Angular2中使用http。

当我发布http帖子时,它会提供正确的请求方法:

enter image description here

然而,当我使用http put时:它将OPTIONS作为请求方法:

enter image description here

我的意思是这是什么意思?

以下是我用于帖子的方法:

  let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
  let options = new RequestOptions({ headers: headers });
  var body = 'amount=' + 12;
  this.http.post(this.host + '/orders', body, options)
  .map(res => res.json())
  .subscribe(data => {
    resolve('success')
  });

这是我用于PUT的方法。

  let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
  let options = new RequestOptions({ headers: headers });
  let body = 'amount=' + 12;
  this.http.put(this.host + '/orders/1', body, options)
  .map(res => res.json())
  .subscribe(data => {
    resolve('success')
  }) 

1 个答案:

答案 0 :(得分:1)

我认为这是因为CORS。它与Angular2无关。对于POST方法,将执行一个简单的方法。在PUT的情况下,如果是预检,那么浏览器将引入一个额外的请求(一个OPTIONS)。似乎你在这个OPTIONS方法上有404。

提醒一下:

  

简单请求。如果我们使用HTTP GET,HEAD和POST方法,则此用例适用。对于POST方法,仅支持具有以下值的内容类型:text / plain,application / x-www-form-urlencoded和multipart / form-data。

     

预检请求。当"简单请求"用例不适用,第一个请求(使用HTTP OPTIONS方法)用于检查在跨域请求的上下文中可以执行的操作。

我认为这些文章会引起你的兴趣: