小编a-m*_*a-m的帖子

Angular 6:无法正确设置 http Header 的 Content-Type

我正在尝试在 Angular 6 中使用 HttpHeader 进行后期调用,并且我将 Content-Type 设置为 application/json。但是服务器为 Content-Type 获取 x-www-form-urlencoded 而不是 application/json。

服务.ts

   
myFunction(id: string, name: string, fields: string[]) {
  const body = {
    id: id,
    name: name,
    fields: fields
  };
  let headers = new HttpHeaders();
  headers= headers.set('content-type', 'application/json');
  return this.http.post(this.URL , body, {headers});
}
Run Code Online (Sandbox Code Playgroud)

组件.ts

submit(){
  this.myService.myFunction(this.id, this.form.value.name,  
  this.form.value.fields).subscribe((res:any) => {
    console.log(this.form);
  }, error => {
    console.log(JSON.parse(error.error).errors);
  })
}
Run Code Online (Sandbox Code Playgroud)

content-type http-headers angular

7
推荐指数
2
解决办法
2万
查看次数

标签 统计

angular ×1

content-type ×1

http-headers ×1