How to get Jquery Ajax Readystates -


How can I know the preparations for an AJAX request via 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 & amp; http.status == 200} {// do something}}   

So I can easily track I can use the code generated from 1 to 4 using the code above, and take the necessary action as if the ready list is 1.

But how to prepare, through a jquery AJAX call Can I track values?

I am making an Ajax call through jquery like this:

  $ .post ('ajax / test.html', Function (data) {$ ('result') .html (data);}); < Take a look at the documentation of / code>   

. You have different callbacks (possibly evil "ready state" But it is enough for an indicator to have the possibility of passing:

Preceded (jqXHR, settings) Function
A pre-requested callback function, used before sending the jqXHR (jQuery 1.4.x, XMLHTTPRequest ) object. Use this to set a custom header, etc. jqXHR and settings are passed as map logic. This is an Ajax event false before sending before the function will cancel the request. According to JQuery 1.5, the preceded option will be called regardless of the type of request.

Success (data, textstats, jxaxhr) function, array
if request to call a function The function has three arguments: the data returned from the server, formatted according to the dataType parameter; A string describing the situation; And jqXHR (in jQuery 1.4.x, XMLHttpRequest ) objects. According to JQuery 1.5, success setting can accept an array of functions. Each ceremony will be said in return, this is one.

Error (jqXHR, textStatus, errorThrown) function
If a function request fails to call The function receives three arguments: jqXHR (in jQuery 1.4.x, XMLHttpRequest ) object, a string that describes the type of error and an optional exception Object, if any one happened. Potential value for the second argument (other than null ) "timeout" , "error" , "abort" , And "parserar" . this one . According to JQuery 1.5, the error setting can accept an array of functions. Each function will be asked in return Note: This handler is not called for cross-domain scripts and JSONP requests.

complete (jqXHR, textStatus) function, array
upon expiration ( success < Code / and error callback will be called to call a function.Function passes two arguments: jqXHR (in jQuery 1.4.x, XMLHTTPRequest ) A string ( "success" / code>, "notmodified" , "error") categorizes the state of the request and request status Code>, "timeout" , "abort" , or "parsererror" ). According to jQuery 1.5, the complete setting can accept an array of functions. Each function will be asked in return, this is the one.

The best idea will be to show the indicator in the callback sent to call before and and hide it in complete .


< Strong> Example:

So you have to rewrite your code:

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

Although I do not understand why you use POST because you are not sending any data (at least in your example code).

Comments