Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

After getting the API response my angular page is not redirecting to another page

So here is my code from ts.Here i call the method updateProduct that makes a request to the API to update the product with the values that are in the form.The update is made but after i receive this error:

   error: 
  SyntaxError: Unexpected token 'P', "Product Up"... is not valid JSON at JSON.parse 
 (<anonymous>) at XMLHttpRequest.onLoad (https://localhost:4200/vendor.js:36220:39) at 
 "Unexpected token 'P', \"Product Up\"... is not valid JSON"
 stack: 
 "SyntaxError: Unexpected token 'P', \"Product Up\"... is not valid JSON\n    at JSON.parse (<anonymous>)\n    at XMLHttpRequest.onLoad (https://localhost:4200/vendor.js:36220:39)\n    

The error is catch in this ts.

this.shopService.updateProduct(this.productForm.value).subscribe(response => {
  this.router.navigateByUrl('/admin');
}, error => {
  console.log(error);
  this.errors = error.errors;
});

Can someone explain what is causing this error to happen?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is the code from the API:
var productMapped = _mapper.Map<ProductDto, Product>(product);

        _productsRepo.Update(productMapped);

        if(await _productsRepo.SaveAll())
        {
            return Ok("Product Updated with succes");
        }

        return BadRequest("Could not update the product");

>Solution :

... is not valid JSON at JSON.parse

is the indication that the http call expects the response to be JSON but could not parse it. You are returning a regular string, so the parser will fail.
If you are not using the response value, you can just return an empty Ok response.
If you want to read the response string, you can change the accept header of your http request to text/plain instead of application/json

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading