【发布时间】:2019-05-06 05:30:55
【问题描述】:
我正在尝试使用 Angular 6 中的 HttpHeader 进行发布调用,并将 Content-Type 设置为 application/json。 但是对于 Content-Type,服务器获取的是 x-www-form-urlencoded 而不是 application/json。
service.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});
}
组件.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);
})
}
【问题讨论】:
标签: angular http-headers content-type