Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery $.ajax and readyStates

How to call the Ajax ready states on the jQuery $.ajax method?

like image 646
tetris Avatar asked Nov 05 '10 16:11

tetris


1 Answers

$.ajax() returns the XmlHttpRequest object, so if you really want to access it as the state changes, you can do this:

var xhr = $.ajax({ ... });
xhr.onreadystatechange = function() { alert(xhr.readyState); };

But the built-in callbacks should be all you need for most uses, particularly success and complete.

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.

like image 155
Nick Craver Avatar answered Sep 23 '22 23:09

Nick Craver