Jump to content

return responseText = undefined?


tinfanide
 Share

Recommended Posts

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

Is there a syntax mistake that produces the undefined result?
Link to comment
Share on other sites

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

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 by niche
Link to comment
Share on other sites

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

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 by Tin
Link to comment
Share on other sites

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...