typescript Angular 中没有正文的 HTTP Post

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47441842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:03:39  来源:igfitidea点击:

HTTP Post with no body in Angular

angularhttptypescriptangular-http

提问by William Hampshire

I'm wondering how to send an HTTP post request without a body (specifically in Angular). Here's what I'm doing now, but I'm getting the error Expected 2-3 arguments, but got 1).

我想知道如何在没有正文的情况下发送 HTTP post 请求(特别是在 Angular 中)。这就是我现在正在做的事情,但我收到了错误Expected 2-3 arguments, but got 1)

I realize the second argument is for the body, but I'm not sending that to the server (yes, I understand that a POST call changes the state of a system and have looked into THISquestion).

我意识到第二个参数是针对主体的,但我没有将其发送到服务器(是的,我知道 POST 调用会更改系统的状态,并且已经研究了这个问题)。

postRequest(id) {
  this.http.post('/api?data=' + id).map(
    (response) => {
      return response;
    }
  )
}

回答by William Hampshire

Looks like this is the appropriate answer:

看起来这是正确的答案:

postRequest(id) {
  this.http.post('/api?data=' + id, null).map(
    (response) => {
      return response;
    }
  )
}

回答by P Satish Patro

If null is not working(4XX error client side), try with {} JSON

如果 null 不起作用(4XX 错误客户端),请尝试使用 {} JSON

  postRequest(id) 
  {
     this.http.post('/api?data=' + id, {}).map((response) => {return response;})
  }