Jump to content

How to check undefined ajax responseText?


Recommended Posts

var ajax = function(query){xmlhttp = (window.XMLHttpRequest) ? xmlhttp=new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ;xmlhttp.onreadystatechange = function(){   jsonData = JSON.parse(xmlhttp.responseText);   (xmlhttp.readyState==4 && xmlhttp.status==200) ?   func(query) : null ;   }xmlhttp.open("GET","lib.json",true);xmlhttp.send();} function func(query){ // fail to check an undefined AJAX responseTextif(typeof(xmlhttp.responseText)=="undefined" && xmlhttp.responseText != null){  alert(0);  }else {  document.getElementById("result").innerHTML = jsonData[1][query];  }}

This is the testing page:http://lifelearning.xtreemhost.com/lib.html Just wanna ask how to check an undefined AJAX responseText.Many thanks.

Link to post
Share on other sites
var ajax = function(query){xmlhttp = (window.XMLHttpRequest) ? xmlhttp=new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP") ? new ActiveXObject("Msxml2.XMLHTTP") : null ;xmlhttp.onreadystatechange = function(){   jsonData = JSON.parse(xmlhttp.responseText);   (xmlhttp.readyState==4 && xmlhttp.status==200) ?   func(query) : null ;   }xmlhttp.open("GET","lib.json",true);xmlhttp.send();} function func(query){ // fail to check an undefined AJAX responseText if(typeof(xmlhttp.responseText)=="undefined" && xmlhttp.responseText != null){  alert(0);  }else {  document.getElementById("result").innerHTML = jsonData[1][query];  }}

This is the testing page:http://lifelearning....st.com/lib.html Just wanna ask how to check an undefined AJAX responseText.Many thanks.

Solve it.It should be
if (typeof(jsonData[1][query])!=="undefined" && (jsonData[1][query])!=null){ // something}

not jsonData or xmlhttp.responseText that should not be null or "undefined"

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