Jump to content

How to check undefined ajax responseText?


tinfanide
 Share

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