how can I do the following by means of jQuery?
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementByID('statusDisplay').innerHTML = xmlhttp.responseText; // show loaded content
} else if (xmlhttp.readyState >= 1 && xmlhttp.status == 200) /* if(xmlhttp.readyState>=2 && xmlhttp.status==200) */ {
document.getElementByID('statusDisplay').innerHTML = '<img src="ajax_load.gif" />'; // show ajax loading image
}
}
xmlhttp.open("GET", "path/to/file.php", true);
xmlhttp.send();
What I am mainly interested in is how I can retrieve the readyStatea and status and how I can retrieve the response text from within those functions (more or less like this):
$.ajax({url: 'path/to/file.php', async: true, success: function(){
// how can I get the responseText here?
}, whileLoading: function(){
// does such a parameter actually exist?
}});
Thanks in advance!
jQuery does not support a “native” (jQuery’ish) access to readyStates.
There is no
interactivecallback for instance which could represent areadyState===3.Anyway, you have access to the
responseTextin thesuccess callbackfrom.ajax()Anyway, the
.ajax()method returns theXMLHttpRequestwhich you can access if necessary.This is a possible workaround of that issue. But in general you will have all data and information you need within the
ajax event callbacks.Furthermore is the
XMLHttpRequest objectpassed into a lot of callbacks, likebeforeSend,errorandsuccess.See http://api.jquery.com/jQuery.ajax/ for further details.