Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting HttpErrorResponse error in angular

I am trying to send http post request from my createorder.component.ts page to create a record. But I am getting HttpErrorResponse error. Where I am doing wrong? please help

createorder.component.ts

onSubmit(form: any) {
this.spinner.show();
 this.http.post('/api/order/' + '_foodforest' + '/' + '9ed7f9d5-8ed3-4f57-b360-7e4da87f2d6d' ,
      JSON.stringify(form.value))
      .subscribe(result => {
this.spinner.hide();
});
}

Error

enter image description here

like image 973
prasannakumar chebrolu Avatar asked Aug 07 '26 13:08

prasannakumar chebrolu


1 Answers

Angular HttpClient try to parse the api response into JSON by default.

Your API response is not in a JSON format and JSON.parse is failed inside HttpClient.

You can return the api response as JSON from backend or should tell the http client that the type of resposne is not a JSON:

this.http.post(url, JSON.stringify(form.value), {
  responseType: 'text'
})
like image 164
topmoon Avatar answered Aug 10 '26 14:08

topmoon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!