POST时HttpClient无法获得响应

时间:2018-08-16 16:42:09

标签: json angular http post

我想从服务器接收响应。与邮递员一起工作很棒: 请求:

// the default template arguments of MyClass will be used
Wrapper<MyClass<uint8_t, 210>> firstWrapper;                 // A=uint8_t, B=210, C=size_t, D=true
Wrapper<MyClass<uint8_t, 210, uint8_t>> secondWrapper;       // A=uint8_t, B=210, C=uint8_t, D=true
Wrapper<MyClass<uint8_t, 210, uint8_t, false>> thirdWrapper; // A=uint8_t, B=210, C=uint8_t, D=false

响应:

{
    "name": "antonio",
    "username": "antonio",
    "email": "antonio@antonio.pl",
    "password": "antonio"
}

我试图在Angular中实现相同的功能,所以我得到了:

{
    "success": true,
    "message": "User was successfully registered"
}

已检查后端API,效果很好,但仍未出现警报。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果您将请求作为“ application / json”发送,则不需要JSON.stringify()。我不知道您的后端如何解释它。

this.http.post('http://localhost:8090/api/auth/signup',
  {name: name, username: username, password: password, email: email},
  {headers: new HttpHeaders({'Content-Type': 'application/json'})})
  .subscribe(res => alert(res[0]));

https://angular.io/api/common/http/HttpClient#post