DEV Community

I can't find where is error . SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

dragos on November 28, 2019

This is my code : window.addEventListener('load', () => { let long; //longitude: let lat; //latitude; let temperatureDescription = ...
Collapse
 
savagepixie profile image
SavagePixie

Before you try response.json, could you log the returned data on your console and give us an idea of what it looks like? It sounds like it's not a valid json.

Collapse
 
ivkemilioner profile image
dragos

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Collapse
 
savagepixie profile image
SavagePixie

But that's the error you're getting, isn't it? Try doing this:

fetch(api)
   .then(response => {
      console.log('Response:', response)
      return response.json();
})

And let us know what you get in the console after Response:

Thread Thread
 
blindfish3 profile image
Ben Calder • Edited

Logging the response will help, but a quicker approach to debugging this would be to run it in the browser with dev tools open on the network tab and confirm that:

  • the request you're making is properly formed: e.g. string parameters are included as expected
  • the response is what you expect: i.e. json

You can see the contents by selecting the response in the list and it'll then show tabs where you can expand details

Thread Thread
 
devdrake0 profile image
Si

It's a different approach, I wouldn't say it's "quicker".

Thread Thread
 
blindfish3 profile image
Ben Calder

There's nothing wrong with console.log, but it soon becomes tedious adding and removing it from your code. That definitely takes longer than hitting
F12 + a couple of mouse clicks to get to the Network tab; which doesn't even require you to open your editor.

Collapse
 
manan30 profile image
Manan Joshi

Maybe your response is empty.

Collapse
 
blindfish3 profile image
Ben Calder

As others have said: the answer will be found by inspecting the response. It could be that you're actually receiving HTML - sometimes you see this when the request URL is blocked or you've passed invalid parameters...

Collapse
 
ivkemilioner profile image
dragos

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data