How to call the Ajax ready states on the jQuery $.ajax
method?
相关问题
- Carriage Return (ASCII chr 13) is missing from tex
- How to fix IE ClearType + jQuery opacity problem i
- jQuery add and remove delay
- Using :remote => true with hover event
- Is there a way to play audio on a mobile browser w
Method, tested with jQuery 2.0.2:
Basically, what I needed was a callback after
readyState
becomes1
(Connected), which, in my case, was useful when implementing long polling "push" notifications with jQuery.You should be able to get all you need by setting callbacks for the
success
,error
, andcomplete
options in the object you pass into theajax()
method. Take a look at the documentation:http://api.jquery.com/jQuery.ajax/
Basically, it works like this:
You can see the docs for exactly what parameters you have access to in the callback functions.
$.ajax()
returns the XmlHttpRequest object, so if you really want to access it as the state changes, you can do this:But the built-in callbacks should be all you need for most uses, particularly
success
andcomplete
.To do things before the request fires, use
beforeSend
, or more appropriately for most cases, the.ajaxStart()
and.ajaxStop()
events...for example to show a loading message whenever any ajax activity is going on.