gpt4 book ai didi

Angular 6 : Unable to set Content-Type of http Header correctly

转载 作者:行者123 更新时间:2023-12-03 15:13:41 24 4
gpt4 key购买 nike

我正在尝试在 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});
}


组件.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);
})
}

最佳答案

您需要在选项中设置标题。

return this.http.post(this.URL , body, {headers : new HttpHeaders({ 'Content-Type': 'application/json' })});

关于 Angular 6 : Unable to set Content-Type of http Header correctly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53618872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com