tinfanide Posted June 16, 2012 Share Posted June 16, 2012 <script>function ajax(){xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ;xmlhttp.onreadystatechange = function(){ var jsonData = eval("("+xmlhttp.responseText+")"); return (this.readyState == 4 && this.status == 200) ? jsonData[1][1] : null ;}xmlhttp.open("GET","json.json",true);xmlhttp.send(null);} window.onload function func(){document.getElementById(elem).innerHTML = ajax(); // return undefined}</script> // json.json[ ["http://hk.yahoo.com","Yahoo HK","News"], ["http://www.google.com","Google","Search Engine"],] Is there a syntax mistake that produces the undefined result? Link to comment Share on other sites More sharing options...
Ingolme Posted June 16, 2012 Share Posted June 16, 2012 You should check that readyState is 4 before using responseText. Link to comment Share on other sites More sharing options...
niche Posted June 16, 2012 Share Posted June 16, 2012 Please see tutorial at http://www.w3schools.com/php/php_ajax_php.asp Link to comment Share on other sites More sharing options...
tinfanide Posted June 17, 2012 Author Share Posted June 17, 2012 The problem lies in, in my honest opinion, the way I return the xmlhttp.responseText.Just ask for help on how to return an ajax object. function ajax(){xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ;xmlhttp.onreadystatechange = function(){ var jsonData = eval("("+xmlhttp.responseText+")"); (this.readyState == 4 && this.status == 200) ? alert(jsonData[0][0]) : null ;}xmlhttp.open("GET","json.json",true);xmlhttp.send(null);} This does the job, though. Link to comment Share on other sites More sharing options...
niche Posted June 17, 2012 Share Posted June 17, 2012 (edited) Per the tutorial and Ingolme's post, this is how you do that: if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } If you're doing that, no problem. Edited June 17, 2012 by niche Link to comment Share on other sites More sharing options...
dsonesuk Posted June 17, 2012 Share Posted June 17, 2012 You are experiencing the same problem discussed here http://w3schools.invisionzone.com/index.php?showtopic=43950&pid=244090&st=0entry244090, if you change xmlhttp.open("GET","json.json",true); to xmlhttp.open("GET","json.json",false); it should work, and return the required value, else try code similar to below var elem = "ThisElementID"function ajax(callback) { xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ; xmlhttp.onreadystatechange = function() { if(this.readyState == 4 && this.status == 200) { jsonData = eval("("+xmlhttp.responseText+")"); callback(jsonData[1][1]); } } xmlhttp.open("GET","json.json",true); xmlhttp.send(null); } window.onload= function() { ajax(function(getJsonData) { document.getElementById(elem).innerHTML = GetJsonData; }); } <div id="ThisElementID"></div> Link to comment Share on other sites More sharing options...
tinfanide Posted June 17, 2012 Author Share Posted June 17, 2012 (edited) Changin true to false (asyn) does not work. Yes, using a callback does the work. I didn't know how to use a callback, though.Just still don't understand why it works like return and why return does not work in this case.Just a typo (GetJsonData -> getJsonData)Thank you. Edited June 17, 2012 by Tin Link to comment Share on other sites More sharing options...
dsonesuk Posted June 17, 2012 Share Posted June 17, 2012 To work with false var elem = "ThisElementID"function ajax() { xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ; xmlhttp.onreadystatechange = function() { if(this.readyState == 4 && this.status == 200) { jsonData = eval("("+xmlhttp.responseText+")"); } } xmlhttp.open("GET","json.json",false); // with false it pauses until value is returned before proceeding which may cause older browsers to lock. xmlhttp.send(null); return jsonData[1][2]; } window.onload= function() { document.getElementById(elem).innerHTML = ajax(); } another option var elem = "ThisElementID"function ajax() { xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ; xmlhttp.onreadystatechange = function() { if(this.readyState == 4 && this.status == 200) { jsonData = eval("("+xmlhttp.responseText+")"); document.getElementById(elem).innerHTML = jsonData[1][2]; } } xmlhttp.open("GET","json.json",true); xmlhttp.send(null); } window.onload= function() { ajax() } Link to comment Share on other sites More sharing options...
tinfanide Posted June 18, 2012 Author Share Posted June 18, 2012 To work with false var elem = "ThisElementID" function ajax() { xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ; xmlhttp.onreadystatechange = function() { if(this.readyState == 4 && this.status == 200) { jsonData = eval("("+xmlhttp.responseText+")"); } } xmlhttp.open("GET","json.json",false); // with false it pauses until value is returned before proceeding which may cause older browsers to lock. xmlhttp.send(null); return jsonData[1][2]; } window.onload= function() { document.getElementById(elem).innerHTML = ajax(); } another option var elem = "ThisElementID" function ajax() { xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ; xmlhttp.onreadystatechange = function() { if(this.readyState == 4 && this.status == 200) { jsonData = eval("("+xmlhttp.responseText+")"); document.getElementById(elem).innerHTML = jsonData[1][2]; } } xmlhttp.open("GET","json.json",true); xmlhttp.send(null); } window.onload= function() { ajax() } Yes, both work. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now