javascript - No output error when attempting to display json -


when following code executes it's showing no output. can tell me going wrong?

html file:

<head> <script type = "text/javascript">function ajax_get_json()      {     var hr = new xmlhttprequest();     hr.open("get","mylist.json",true);     hr.setrequestheader("content-type :application/json",true);      hr.onread       ystatechange = 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);     results.innerhtml = "requesting...";     }   </script>  </head>  <body>      <div id="results"></div>     <script type = "text/javascript">ajax_get_json();</script>  </body> 

json file: { "user":"john", "age":22, "country":"us" }

xmlhttprequest() must xmlhttprequest(). javascript file looks like:

function request() {}  request(); 

html

<script src="file.js"></script> 

with var data = json.parse(hr.responsetext); have on object. go through object:

for(var key in obj) {} 

and store key , obj[key] in innerhtml see result. complete javascript file:

function request() {   var hr = new xmlhttprequest();   hr.onreadystatechange = function() {     if (hr.readystate == 4 && hr.status == 200) {       var data = json.parse(hr.responsetext); // object       var results = document.getelementbyid("results");       results.innerhtml = "";       for(var key in data) {         results.innerhtml += key + " " + data[key] + "<br>";       }      }   }   hr.open("get", "mylist.json", true);   hr.send();   results.innerhtml = "requesting..."; }  request(); 

(please change title of question. specific! how ask)


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -