How to get Jquery Ajax Readistates

How can I find out how to read ajax request through jquery? In the general case, without using jquery, we will send an ajax request as follows:

http.open("POST", url, true);

http.onreadystatechange = function() { 
    if(http.readyState == 4 && http.status == 200) {
        // do something
    }
}

This way, I can easily track the readystate value from 1 to 4 using the above code and perform the necessary actions, for example, showing the download icon when readystate is 1.

But through a jquery ajax call, how can I track readystate values?

I am making an ajax call through jquery as follows:

$.post('ajax/test.html', function(data) {
  $('.result').html(data);
});
+5
source share
3 answers

$.ajax. (, " " evert, ):

beforeSend(jqXHR, settings)
, jqXHR ( jQuery 1.4.x, XMLHTTPRequest) . .. jqXHR settings . Ajax. false beforeSend . jQuery 1.5, beforeSend .

success(data, textStatus, jqXHR) ,
, . : data, , dataType; , ; jqXHR ( jQuery 1.4.x, XMLHTTPRequest). jQuery 1.5, . . Ajax Event.

error(jqXHR, textStatus, errorThrown)
, , . : jqXHR ( jQuery 1.4.x, XMLHTTPRequest), , , . ( null) "timeout", "error", "abort" "parsererror". Ajax Event. jQuery 1.5, error . . . script JSONP.

complete(jqXHR, textStatus) ,
, ( success error ). : jqXHR ( jQuery 1.4.x, XMLHTTPRequest) , ("success", "notmodified", "error", "timeout", "abort" "parsererror"). jQuery 1.5, complete . . Ajax Event.

, , beforeSend, complete.


:

, :

$.ajax({
  url: 'ajax/test.html',
  type: 'POST',
  beforeSend: function() {
       // show indicator
  },
  complete: function() {
      // hide indicator
  },
  success: function(data) {
      $('.result').html(data);
  }
});

, POST, - ( , ).

+11

$.ajax({
 url:,
success:function(){

}//This is ready state 4
});

perform necessary action like showing a loading icon when readystate is 1.

ajaxStart ajaxStop

+1

http://api.jquery.com/category/ajax/ I think you will find that the different events here correspond to the different states of ReadyStates.

0
source

All Articles