how can i know the readystates of an ajax request through jquery ? In general, without using jquery, we will send an ajax request like this:
http.open("POST", url, true);
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
// do something
}
}
So i can easily track the readystate value from 1 to 4 using the above code, and perform necessary action like showing a loading icon when readystate is 1.
But through a jquery ajax call, how can i track the readystate values ?
I am making an ajax call through jquery like this:
$.post('ajax/test.html', function(data) {
$('.result').html(data);
});
In this article, we are going to learn about the readyStates of a request in AJAX. readyState is an XMLHttpRequest property. How to use readyState?
jQuery - AJAX get() and post() Methods. The jQuery get() and post() methods are used to request data from the server with an HTTP GET or POST request. HTTP Request: GET vs. POST. Two commonly used methods for a request-response between a client and server are: GET and POST.
AJAX stands for “ A synchronous J avaScript and X ML”. In simpler words, you can use Ajax to load data from backend without actually the page reloading. You can also send data to the server in the background, request data and receive data while the page has already loaded.
"that means the request has sent successfully."; readyState=3: This state shows that the request is still in the process and the data hold by responseText, which means we can access the data, while the request isn’t finished or complete yet. But at this stage, the data we get may or may not be complete as the request is still in the process.
Have a look at the documentation of $.ajax
. You have the possibility to pass different callbacks (maybe not for evert "ready state" but it is enough for an indicator):
beforeSend(jqXHR, settings)
Function
A pre-request callback function that can be used to modify thejqXHR
(in jQuery 1.4.x,XMLHTTPRequest
) object before it is sent. Use this to set custom headers, etc. ThejqXHR
andsettings
maps are passed as arguments. This is an Ajax Event. Returningfalse
in thebeforeSend
function will cancel the request. As of jQuery 1.5, thebeforeSend
option will be called regardless of the type of request.
success(data, textStatus, jqXHR)
Function, Array
A function to be called if the request succeeds. The function gets passed three arguments: Thedata
returned from the server, formatted according to thedataType
parameter; a string describing the status; and thejqXHR
(in jQuery 1.4.x,XMLHttpRequest
) object. As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.
error(jqXHR, textStatus, errorThrown)
Function
A function to be called if the request fails. The function receives three arguments: ThejqXHR
(in jQuery 1.4.x,XMLHttpRequest
) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besidesnull
) are"timeout"
,"error"
,"abort"
, and"parsererror"
. This is an Ajax Event. As of jQuery 1.5, theerror
setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and JSONP requests.
complete(jqXHR, textStatus)
Function, Array
A function to be called when the request finishes (aftersuccess
anderror
callbacks are executed). The function gets passed two arguments: ThejqXHR
(in jQuery 1.4.x,XMLHTTPRequest
) object and a string categorizing the status of the request ("success"
,"notmodified"
,"error"
,"timeout"
,"abort"
, or"parsererror"
). As of jQuery 1.5, thecomplete
setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.
So the best idea would be to show the indicator in the callback passed to beforeSend
and hide it in complete
.
Example:
So you would have to rewrite your code as:
$.ajax({
url: 'ajax/test.html',
type: 'POST',
beforeSend: function() {
// show indicator
},
complete: function() {
// hide indicator
},
success: function(data) {
$('.result').html(data);
}
});
although I don't understand why you use POST as your are not sending any data (at least in your example code).
use
$.ajax({
url:,
success:function(){
}//This is ready state 4
});
perform necessary action like showing a loading icon when readystate is 1.
for that you may use ajaxStart and ajaxStop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With