角度6:无法正确设置HTTP标头的内容类型 [英] Angular 6: Unable to set Content-Type of http Header correctly
本文介绍了角度6:无法正确设置HTTP标头的内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Angular 6中的HttpHeader进行发布呼叫,并将Content-Type设置为application / json。
但是服务器获取的Content-Type是x-www-form-urlencoded而不是application / json。
I'm trying to make a post call using HttpHeader in angular 6 And I set Content-Type to application/json. But the server get x-www-form-urlencoded instead of application/json for Content-Type.
service.ts
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});
}
component.ts
component.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);
})
}
推荐答案
您需要在选项中设置标题。
You need to set headers in option.
return this.http.post(this.URL , body, {headers : new HttpHeaders({ 'Content-Type': 'application/json' })});
这篇关于角度6:无法正确设置HTTP标头的内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文