首页 > 解决方案 > 加载资源失败:服务器响应状态为 404 (Not Found) ERROR HttpErrorResponse

问题描述

我目前正在开发一个 Angular 7 应用程序。当我尝试调用http post时,当web api返回成功消息时它工作正常,当api返回错误消息时我得到错误。消息返回类似于 {Error=true, Message="invalid data"}

错误按摩图像

服务.ts

addCustomer(customer: Customer) {
        const body: Customer = {
           // do...
        };
        return this.http.post(this.apiurl + 'Products/Post', body);
    }

组件.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`
      }
    });
  }

标签: angular

解决方案


我认为这是一个后端问题。如果您对后端大张旗鼓,请尝试在其上使用无效数据并查看响应。

更新 试试这个方法。我删除了,===false因为您的第一条语句必须返回成功消息!else 语句处理错误!

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);
  }
});

}


推荐阅读