我目前正在开发Angular 7应用程序。当我尝试调用http发布时,当网络api返回成功消息时,它工作正常,当api返回错误消息时,我得到了错误。消息返回类似{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`
}
});
}