Jump to content

return responseText = undefined?


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 post
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 post
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 post
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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...