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