SyntaxError: Unexpected token ‘<‘, ≤br /≥ ≤b≥… is not valid JSON

If you’re getting the error “SyntaxError: Unexpected token ‘<‘, “<br /><b>”… is not valid JSON”, it’s probably because the server is giving an HTML page with an error as response. The best way to fix the problem is to identify what’s the problem server-side: change response.json() with response.text() and treat the consequent data with JSON.parse(text) in a try…catch statement.

Before:

  //fetch() ...
  .then(response => response.json())
  .then(data => {
  //...

After:

  //fetch() ...
  .then(response => response.text())
  .then(data => {     try {
    console.log(JSON.parse(data));
  } catch (e) {
    console.error('Error parsing JSON:', data);
  }
  //...

Most probably you will now be able to read in the developer console (F12) what the response is, being able to identify the server-side problem to fix.

SyntaxError: Unexpected token '<', "<br /><b>"… is not valid JSON

If this wasn’t helpful yet, try give a look on this StackOverflow thread.

Without ads, you wouldn’t be able to access this content because we wouldn’t be able to cover domain and hosting costs.

Please, disable the Ads Blocker to support us and we’ll repay with our content.
Thank you 🙂