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