在将数据从 angular 发送到 spring 引导后,无法将数据转换为 json in angular 13 内容类型 'text/plain;charset=UTF-8' 不支持]
Cannot convert data to json in angular 13 Content type 'text/plain;charset=UTF-8' not supported] after sending data from angular to spring boot
我正在尝试通过以下方式提交表单:
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});
}
毕竟我得到了标题中的异常。我做错了什么?
如果您要发送表单数据,请将“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});
}
You can Change "Content-Type" to application/x-www-form-urlencoded and "Accept" application/json. and try this below is the code snip.
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});
}
总结一下我上面的悲伤。实际的创建函数是这样的。这是发送正确的 header 信息:
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});
}
我正在尝试通过以下方式提交表单:
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});
}
毕竟我得到了标题中的异常。我做错了什么?
如果您要发送表单数据,请将“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});
}
You can Change "Content-Type" to application/x-www-form-urlencoded and "Accept" application/json. and try this below is the code snip.
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});
}
总结一下我上面的悲伤。实际的创建函数是这样的。这是发送正确的 header 信息:
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});
}