Proposal: binding custon xhr.onreadystatechange handler in jQuery 1.4
We're currently looking at upgrading our application from jQuery 1.3 to 1.4 and are running in to an issue with the change in jQuery.ajax to use onreadystatechange directly instead of setInterval. In our code, in ajax beforeSend, we are binding our own onreadystatechange handler to inspect the xhr in readyState=4 to handle the response in advance of success/failure and complete being called:
- jQuery.ajax($.extend({
- beforeSend: function(xhr) {
- xhr.onreadystatechange = function() {
- if (xhr.readyState == 4) { //headers are ready
- if ("LOGIN" == xhr.getResponseHeader("LOGIN")) {
- xhr.abort();
- navigateToLoginPage();
- }
- // etc...
- }
- };
- }, requestSettings);
Due to the change, our handler is never being called, and I can't see any way to replicate the behavior. Moving our code to complete() wouldn't suffice since it needs to be handled before success/failure. Is there an advisable solution here?
One proposal would be to add an onreadystatechange handler to jQuery.ajax that would allow custom work to be done in response to readyState change. This would behave similarly to beforeSend, in that the function could return false to abort. I can submit a patch if this would be considered useful:
- //In jQuery.ajax function, line 5163
- var onreadystatechange = xhr.onreadystatechange = function( isTimeout ) {
- if ( s.onreadystatechange && s.onreadystatechange.call(callbackContext, xhr, s) === false ) {
- complete();
- if ( xhr ) {
- xhr.onreadystatechange = jQuery.noop;
- }
- }
- ///...
Thanks,
Jason