var jqxhr = $.getJSON( "main.json", function(data) return data;);var json;home window.onload = function() var jsonTxt = jqxhr.responseText; jkid = JSON.parse(jsonTxt); ....But eexceptionally 10th attempt or so i acquire the adhering to error:

Uncaptured SyntaxError: Unexpected token u in JSON at place 0at JSON.parse ()at home window.onfill (profile.js:8)It"s really annoying, bereason it will leave my page blank. I perform think it has actually something to carry out via me trying to parse the JSON wrong - or something. I really do not recognize and I"m looking forward to any type of sort of answer that can lead me to the settle. Thank you.

You are watching: Syntaxerror: unexpected token u in json at position 0


*

*

That unsupposed "u" is the first letter of the string "undefined". It happens because your 2 asynchronous operations (i.e. loading the JSON and also loading the window) are racing to completion, and also if the JSON isn"t loaded fast sufficient, the window.onfill method will attempt to parse the JSON string that isn"t loaded yet.

A solution is to move the JSON loading inside the onfill even handler. Furthermore, the whole suggest of getJSON is that it currently parses the response for you as a JSON object, so tbelow is no require for JSON.parse():

home window.onpack = function() $.getJSON( "main.json", function(json) // process the results here );

*

Insert a shot capture block wbelow the JSON.parse() is referred to as.

It is possible that the message itself is not JSON.

For example...

I think this is the problem bereason if response message is unidentified, the char "u" is at place 0. So JSON.parse() is functioning through a string. Not a javascript-object-notation text file.

if(response !== undefined) try // do parse catch(errror) console.error("Not a JSON response")

*

Thanks for contributing an answer to Stack Overflow!

Please be sure to answer the question. Provide details and also share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based upon opinion; ago them up with recommendations or personal experience.

See more: Best Fish And Chips Salt Lake City, Ut, Fish And Chips Restaurants In Salt Lake City

To learn even more, check out our tips on writing good answers.


Message Your Answer Discard

By clicking “Post Your Answer”, you agree to our regards to service, privacy policy and also cookie plan


Not the answer you're looking for? Browse various other questions tagged javamanuscript jkid or ask your own question.


*

website architecture / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2021.9.16.40232


Your privacy

By clicking “Accept all cookies”, you agree Stack Exreadjust have the right to save cookies on your device and also disclose indevelopment in accordance with our Cookie Policy.