Issue
I am trying to submit a form in following way:
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)
});
}
The service is as follows:
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});
}
After all I get the exception like in a title. What I am doing wrong?
Solution
Change "Content-Type" to application/x-www-form-urlencoded
and "Accept" application/json
if you are sending form data.
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});
}
or
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});
}
Answered By - ebduhon
Answer Checked By - Terry (JavaFixing Volunteer)