http.GET有效,但http.post& http.put没有

时间:2018-05-05 19:27:53

标签: asp.net angular http

我正在尝试编写一个与ASP.net后端通信的Angular 4应用程序。 ' GET'请求工作得很好,但我无法进行POST',' PUT'或者'删除'要求工作。以下是一些示例代码:

//asp.net server code:
    [OperationContract]
    [WebInvoke(Method = "POST")]
    public string Post2U([FromUri]string name) { return "name: " + name; }

    [OperationContract]
    [WebInvoke(Method = "POST")]
    public string Post2B([FromBody]string name) { return "name: " + name; }


//Angular code
Post2B(){
  return this.http.post(this.url + "Post2B", {name: 'pops'});
  }

Post2U(){
  return this.http.post(this.url + "Post2U?name=jack");
  }

Post2U()没有错误,但返回字符串只是:"名称:"输入参数未被识别。

Post2B()返回' 405方法不允许'错误

有什么想法吗?感谢。

1 个答案:

答案 0 :(得分:0)

尝试使用JSON.stringify(your_object)对主体进行字符串化,并将{body: body_object}作为第二个参数传递给hhtp.post。

this.http.post(this.url + "Post2B", {body:JSON.stringify({name: 'pops'})})