简体   繁体   English

Angular 后调用不起作用(收到 415 错误)

[英]Angular post call not working (Getting 415 error)

I'm trying to fetch the response from the below code.我正在尝试从以下代码中获取响应。 Which gives the JSON output. In server i'm getting 415 issue and the response also not coming.这给出了 JSON output。在服务器中我收到415问题并且响应也没有到来。

 constructor(private http: HttpClient){}

 public getReports(postData: IRreq) {

    let headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');

    return this.http.post(sampleurl, JSON.stringify(postData), { headers: headers })    
    .pipe(map((res:any) => this.report = res));

};

回复

No issue from the API, getting 200 and response in postman. Not sure what was the issue here, need some help on this. API 没有问题,在 postman 中得到 200 和响应。不确定这里的问题是什么,需要一些帮助。 Thanks谢谢

You are using example for Angular 2. That times json content-type was not standard way.您正在使用 Angular 2 的示例。当时 json 内容类型不是标准方式。

Now when you want to make request with json content-type you only do following:现在,当您想要使用 json 内容类型发出请求时,您只需执行以下操作:

const body = { title: "Angular PUT Request Example" };
this.http
  .put<any>("https://jsonplaceholder.typicode.com/posts/1", body)
  .subscribe((data) => console.warn(data));

So you don't need to do any magic with json encoding etc.所以你不需要对 json 编码等做任何魔术。

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

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