Reputation: 89
I have written a simple rest service as a backend, which I call from my frontend Angular webpage. Below is the frontend code that makes the call. Wether or not a valid person is given, the promise gets rejected and the error message is shown.
savePerson(person: Person) {
const jsonHeader = 'application/json';
const jsonPerson = JSON.stringify(person);
return new Promise((resolve, reject) => {
this.httpClient.post(this.url, jsonPerson, {
headers: new HttpHeaders()
.set('Accept', jsonHeader)
.set('Content-Type', jsonHeader)
})
.toPromise()
.then(
res => { // Success
console.log("success");
resolve(res);
},
res => {
console.log("rejected");
reject(res)
}
);
});
}
This code get called after a button is pressed by the user:
savePerson() {
this.myService.savePerson(this.person).then(
result => {
this.showMessage("Saved");
},
error => {
console.log(error);
this.showMessage("Error when saving")
}
);
}
The logged error (with valid person inputted): Object { headers: {…}, status: 201, statusText: "Created", url: "http://localhost:8080/api/v1.0/person", ok: false, name: "HttpErrorResponse", message: "Http failure during parsing for http://localhost:8080/api/v1.0/person", error: {…} }
I'm assuming this is a problem with the frontend app, since the backend returns a HTTP 201 created. It would be great if anyone could identify the problem for me!
Upvotes: 0
Views: 302
Reputation: 5199
Angular is trying to parse the HTTP response and your server is (I guess) answering with a blank HTTP response. Telling Angular the response is just text should fix the error :
this.httpClient.post(this.url, jsonPerson, {
responseType: 'text',
headers: new HttpHeaders()
.set('Accept', jsonHeader)
.set('Content-Type', jsonHeader)
})
Upvotes: 1