javascript - I am trying to call a json file but it is not working -
i expecting see john printed out json file appended html because of results.innerhtml = data.user;
. using web hosting server open file nothing locally there.
however getting printed out hello world
, document.write
being processed not call ajax function. why happening ?
<!doctype html> <head> <script type="text/javascript"> document.write("hello world") function ajax_get_json(){ var hr = new xmlhttprequest(); hr.open("get", mylist.json, true); hr.setrequestheader("content-type", "application/json", true); hr.onreadystatechange = function() { if(hr.readystate == 4 && hr.status == 200) { var data = json.parse(hr.responsetext); var results = document.getelementbyid("results"); results.innerhtml = data.user; } } hr.send(null); request.innerhtml = "processing..."; } </script> </head> <body> <div id="results"></div> <script type="text/javascript"> ajax_get_json();</script> </body> </html>
and here mylist.json file
:
{ "user": "john", "age":22, "country":"united states" }
it should be
hr.open("get", "mylist.json", true);
the hr.open
second parameter should string contains url of file
Comments
Post a Comment