简体   繁体   English

发送除 GET 之外的 http 请求时没有响应或没有状态代码

[英]No response or no status Code on sending http requests other thatn GET

I have created a rest API with Node.js (locally), tested all of end points with postman and all endpoints works.我已经使用 Node.js(本地)创建了一个 rest API,使用 postman 测试了所有端点并且所有端点都可以工作。

I have created Angular app and trying to hit those endpoints with http requests.我创建了 Angular 应用程序并尝试使用 http 请求访问这些端点。 Any http request other than GET is not working (I have tried POST,PATCH,DELETE ) and the same error.除了 GET 之外的任何 http 请求都不起作用(我尝试过 POST,PATCH,DELETE )和相同的错误。

When I checked the Network tab on Chrome, the request was sent but no response and there is no request Preview or Response in the network tap当我检查 Chrome 上的网络选项卡时,请求已发送但没有响应,并且网络点击中没有请求预览响应

请求有效载荷

请求有效载荷

Here is the Angular method to send POST request and I have subscribed to it of course:这是发送 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"
    }})
}

I hope anyone can help me thanks in advance!我希望任何人都可以帮助我在此先感谢!

the content type is application/json you don't need to stringify the request also remove the <response> .内容类型是 application/json 您不需要将请求字符串化也删除<response>

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"
    }})
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM