首页 > 解决方案 > POST时HttpClient没有得到响应

问题描述

我想从服务器接收响应。使用 Postman 效果很好: 请求:

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

回复:

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

我试图在 Angular 中实现同样的东西,所以我得到了:

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

检查后端 API,效果很好,但仍然没有出现警报。有任何想法吗?

标签: jsonangularhttppost

解决方案


如果您将请求作为“应用程序/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


推荐阅读