Reputation: 389
When I send a POST request I always get the error: "JSON.parse: unexpected end of data at line 1 column 1 of the JSON data", but the data will be created in the database
The network tab will show a Status code 201; when I press modify & resend, the request content is a valid JSON
{"firstName":"t","lastName":"t","password":"t","confirmPassword":"t","year":2018,"month":2,"day":28,"email":"t"}
This is the POST request:
let options = {
headers: new HttpHeaders({'Content-type': 'application/json', 'Accept': 'application/json, */*'})
};
let registerUser = <RegisterUser>({
firstName: this.model.firstName,
lastName: this.model.lastName,
password: this.model.password,
confirmPassword: this.model.confirmPassword,
year: parseInt(this.birthday.split('-')[0], 10),
month: parseInt(this.birthday.split('-')[1], 10),
day: parseInt(this.birthday.split('-')[2], 10),
email: this.model.email
});
this.http.post(this.registerUrl, registerUser, options)
.map((res: Response) => res.json())
.subscribe(data => {
console.log("subscribe: " + data);
},
error => {
console.log("error: ", error);
},
() => {
console.log("Complete");
this.navCtrl.pop();
});
And the stacktrace:
error: Object { headers: {…}, status: 201, statusText: "OK", url: "https://music-makers.herokuapp.com/user/register", ok: false, name: "HttpErrorResponse", message: "Http failure during parsing for https://music-makers.herokuapp.com/user/register", error: {…} } register.ts:86:12
Upvotes: 0
Views: 673
Reputation: 389
Found it, the backend needs to send a 204 no content response instead a 201. This is a bug in Angular
Or send an empty JSON back, but we didn't test this.
Upvotes: 1
Reputation: 1
Try remove block:
.map((res: Response) => res.json())
After change:
this.http.post(this.registerUrl, registerUser, options)
.subscribe(data => {
console.log("subscribe: " + data);
},
error => {
console.log("error: ", error);
},
() => {
console.log("Complete");
this.navCtrl.pop();
});
Upvotes: 0