【问题标题】:No response or no status Code on sending http requests other thatn GET发送 GET 以外的 http 请求时无响应或无状态码
【发布时间】:2020-06-20 11:15:58
【问题描述】:

我已经使用 Node.js(本地)创建了一个 rest API,使用 postman 测试了所有端点并且所有端点都可以正常工作。

我创建了 Angular 应用程序并尝试使用 http 请求访问这些端点。 除 GET 之外的任何 http 请求都不起作用(我尝试过 POST,PATCH,DELETE )并且出现同样的错误。

当我检查 Chrome 上的 Network 选项卡时,请求已发送但没有响应,并且没有请求 PreviewResponse 在网络水龙头

这是发送 POST 请求的 Angular 方法,我当然订阅了它:

addPost(post){
  return this.http.post<response>(`http://localhost:8080/posts/add-post`,
    JSON.stringify({
      title:"How to be Fit",
      content:"By Sports"
    }),{headers:{
      "Content-Type":"application/json"
    }})
}

希望有人能帮助我提前谢谢!

【问题讨论】:

  • 嗯,因为后端在本地运行,您可以调试发送 post 请求时执行的函数。您可以检查正文是否正确发送。
  • 我不知道它是否能解决你的问题,但你不需要把你的身体串起来。
  • 我尝试使用 fetch Api 发布请求成功发生但后端收到的请求正文为空我不知道为什么,然后我尝试使用 fetch api 删除和修补上述问题仍然发生。如果有人可以帮助我吗?

标签: node.js angular http


【解决方案1】:

内容类型是 application/json 您不需要对请求进行字符串化也删除 &lt;response&gt;

addPost(post){
 // this http is the object of HttpClient(import { HttpClient, HttpHeaders } from '@angular/common/http';)
  return this.http.post(`http://localhost:8080/posts/add-post`,
    {
      title:"How to be Fit",
      content:"By Sports"
    },{headers:{
      "Content-Type":"application/json"
    }})
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 2021-08-28
    • 2017-04-16
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多