我正在嘗試通過以下方式提交表單:
saveBuildcompany(): void {
// @ts-ignore
// @ts-ignore
console.log(this.group?.value);
let data2=this.group.value;
let serializedForm = JSON.stringify(data2);
console.log(data2);
// data2.sociallinks.stringify;
this.buildcompanyService.create(serializedForm)
.subscribe({
next: (res) => {
console.log(res);
this.submitted = true;
},
error: (e) => console.error(e)
});
}
服務如下:
create(data: any): Observable<any> {
let headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
return this.http.post(baseUrl "/add", data, {headers: headers});
}
畢竟我得到了標題中的例外。我做錯了什么?
uj5u.com熱心網友回復:
如果您要發送表單資料,請將“Content-Type”更改為application/x-www-form-urlencoded“Accept” application/json。
create(data: any): Observable<any> {
let headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'});
return this.http.post(baseUrl "/add", data, {headers: headers});
}
或者
create(data: any): Observable<any> {
let headers = new HttpHeaders();
headers.set('content-type', 'application/x-www-form-urlencoded')'
...
return this.http.post(baseUrl "/add", data, {headers: headers});
}
uj5u.com熱心網友回復:
您可以將“Content-Type”更改為application/x-www-form-urlencoded和“Accept” application/json。并嘗試以下是代碼片段。
create(data: any): Observable<any> {
let headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'});
return this.http.post(baseUrl "/add", data, {headers: headers});
}
uj5u.com熱心網友回復:
只是總結一下我上面的所有悲傷。實際的創建函式是這樣的。這是發送正確的標頭資訊:
create(data: any): Observable<any> {
let headers = new HttpHeaders({'Content-Type': 'application/json', 'Accept': 'application/json'});
return this.http.post(baseUrl "/add", data, {headers: headers});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/416647.html
標籤:
上一篇:如何獲取請求-WOOF
下一篇:將回應文本決議為json物件
