繁体   English   中英

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

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

我正在尝试从以下代码中获取响应。 这给出了 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));

};

回复

API 没有问题,在 postman 中得到 200 和响应。不确定这里的问题是什么,需要一些帮助。 谢谢

您正在使用 Angular 2 的示例。当时 json 内容类型不是标准方式。

现在,当您想要使用 json 内容类型发出请求时,您只需执行以下操作:

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

所以你不需要对 json 编码等做任何魔术。

暂无
暂无

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

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