Reputation: 33
I am currently developing an Angular 7 application. When I try to call http post it works fine when the web api returns success message, i get error when api returns error message. message returns like {Error=true, Message="invalid data"}
service.ts
addCustomer(customer: Customer) {
const body: Customer = {
// do...
};
return this.http.post(this.apiurl + 'Products/Post', body);
}
component.ts
OnSubmit(form: NgForm) {
this.apiservice.addCustomer(form.value).subscribe((data: any) => {
if (data.Error === false) {
this.resetForm(form);
this.toastr.success(data.Message);
} else {
this.toastr.error(data.Message);`enter code here`
}
});
}
Upvotes: 0
Views: 464
Reputation: 525
I think this is a back-end issue. If you have swagger for the back-end try invalid data on it and see the response.
update
Try this way. I removed the ===false
because your first statement must return a success message! the else statement handles the error!
OnSubmit(form: NgForm) {
this.apiservice.addCustomer(form.value).subscribe((data: any) => {
if (data.Error) {
this.resetForm(form);
this.toastr.success(data.Message);
} else {
this.toastr.error(data.Message);
}
});
}
Upvotes: 1