Mes Fine
Mes Fine

Reputation: 33

Failed to load resource: the server responded with a status of 404 (Not Found) ERROR HttpErrorResponse

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"}

error massege image

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

Answers (1)

abrsh
abrsh

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

Related Questions