It would be helpful to know what that “some html” you are talking about actually looks like.
At a glance, your code is actually missing the first A in AJAX. The first A stands for asynchronous which means that your code should continue to execute while the request is being process.
Now think this: what would happen in your script if the request would take a minute (or an hour) ? You call the open() function, then the request is processed (for an hour).
Your javascript moves to the next instruction which checks for a responseText. However since the request is still being processed, there is no response text (first check fails, second check is true, since the null responseText is different than “none”). So you get a HTML (probably representing the state of the request).
Normally, you must use this piece of code to treat AJAX response
oXMLHTTP.onreadystatechange=function() {
if (oXMLHTTP.readyState==4) {
if (oXMLHTTP.responseText == “none”) {
alert(“No Values”);
} else if (oXMLHTTP.responseText != “none”) {
alert(oXMLHTTP.responseText);
}
}
}
This function will be called everytime the state of the request changes (while the rest of the js will continue to run). A state of 4 means the request was complete and has returned a result.
In your particular case (when you are probably running the script on your local server) when you don’t have results in the query the return is nearly instantaneous so you actually have a responseText on the next line. But otherwise when the server will spend some time processing the query results, you won’t so you must rely on that readyState.
You must be logged in to post a comment.
Comments
Leave a comment Trackback