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?
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