I am relatively new to JavaScript and I am trying to learn to extract data from a .json file. Is the code below correct?
-
<!DOCTYPE html> <html> <head> </head> <body> <h2>JSON Object Creation in JavaScript</h2> <p> First Name: <span id="fname"></span><br /> Last Name: <span id="lname"></span><br /> </p> <script type="text/javascript" src="JSONFile.json"></script> <script> var xhr = new XMLHttpRequest(); xhr.open("GET", "JSONFile.json", true); xhr.send(null); alert("j"); var jsonString = JSON.parse(xhr.responseText); alert("j"); alert(jsonString.properties.id.description); document.getElementById("fname").innerHTML= jsonString.properties.id.description; alert(jsonString.properties.id.description); </script> </body> </html> Here, I am creating an XMLHttpRequest object and trying to fetch the data from the .json file. I have read a lot of blogs and finally came up with this code. The pproblem here is that - if i remove the alert statements in the code, the "jsonString" becomes undefined. with all the alert statements, the code is working properly. What can be the issue over here? I have tested this on Firefox 29.0.1 (firebug installed). P.S. : The code does not work at all in Google chrome.
-
Answer:
You need to move the response handling code to the Ajax success callback. var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { // Ready state 4 means the request is done and // 200 is a successful return if (xhr.readyState === 4 && xhr.status === 200) { var jsonString = JSON.parse(xhr.responseText); document.getElementById("fname").innerHTML= jsonString.properties.id.description; } }; xhr.open("GET", "JSONFile.json", true); xhr.send(null); Since alert() statements block the execution of the code, the Ajax call meanwhile gets completed and your code works fine. Due to asynchronous processing (you can try setting the third parameter to false for the xhr.open() call to make the request synchronous - which beats the purpose of using Ajax and is not recommended), the XHR response handling part in your original code gets processed without waiting for the response; hence the callback.
Kumar Bhot at Quora Visit the source
Related Q & A:
- How to set value in the dropdown from a JSON data list?Best solution by Stack Overflow
- How to extract frequency range from a Wav file?Best solution by Stack Overflow
- How can I extract data of an external XML to PHP?Best solution by Stack Overflow
- How can I transfer all of my Yahoo email files to a document file in my documents?Best solution by Yahoo! Answers
- I cannot get to my addresses when I am trying to forward my e-mails.Best solution by answers.microsoft.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.