Ticket #9883 (assigned feature)
Allow users to respond to other ajax event states.
| Reported by: | hungerandthirst@… | Owned by: | jaubourg |
|---|---|---|---|
| Priority: | low | Milestone: | None |
| Component: | ajax | Version: | git |
| Keywords: | 1.9-discuss | Cc: | |
| Blocking: | Blocked by: |
Description
The jQuery docs explicitly state that "No onreadystatechange mechanism is provided, however, since success, error, complete and statusCode cover all conceivable requirements."
Frankly I think that's limiting users unnecessarily. What if I want to respond to something when I get headers back? What if I want to know when I start receiving content back from the server?
An excellent example of this is using jQuery for bandwidth testing. Without a way to detect when I initially receive content back from the server (not just beforeSend), I will also include any delay spent waiting for the server to respond.
I've added support for the RECEIVE_HEADER and LOADING event as per the XMLHttpRequest specs in this pull request.
I would also like to see support for onreadystatechange as well since those are valuable states that we are only limiting ourselves and losing value by not tracking.
Change History
comment:1 Changed 22 months ago by jaubourg
- Owner set to jaubourg
- Priority changed from undecided to low
- Status changed from new to assigned
- Component changed from unfiled to ajax
- Milestone changed from None to 1.next
comment:4 Changed 9 months ago by dmethvin
- Type changed from enhancement to feature
Bulk change from enhancement to feature.
comment:7 Changed 8 months ago by hungerandthirst@…
Don't know off the top. I will need to investigate and get back to you. Have not had a chance to look into the new callbacks yet.
comment:9 Changed 7 months ago by gnarf
+1, I think we should at least look at using .notify to send something about this state change - Measure the size, and then decide
comment:11 Changed 6 months ago by jaubourg
FYI, preliminary study indicates adding a minimal implementation of a progress callback which the xhr transport would call at each readystate with the native xhr instance as sole parameter (so that it can be inspected and used) is in the vicinity of 30bytes min/gzipped. Any attempt to make it more secure (not expose the native xhr directly) or more useful (call the progress handlers with meaningful parameters) can only make it much bigger and is actually the desirable approach.
I'm quite torn here, because I know how useful it would be yet I feel like it will be too much weight at this point. Since I know the xhr transport will be extremely simplified for 2.x, I'd like to postpone this.
comment:12 Changed 6 months ago by dmethvin
- Milestone changed from 1.9 to None
I'm fine with moving this to a later date. We'll have plenty of other things to keep us busy in the next couple of months. :)
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

First of all, onreadystatechange and xhr events support was removed from the original rewrite because:
The jqXHR object doesn't strive to implement the whole xhr API. It's as complete an xhr-compatible wrapper as it makes sense relatively to the diversity of transports under the hood.
Your bandwidth detection example is clearly not a typical use-case for a web app (and that's putting it mildly). Beside, if you wanted to have full control over the xhr, you can still use jQuery.ajaxSettings.xhr() to create a native xhr (because, in the end, given the level of control you require, you don't need ajax to intervene on top of the native xhr -- by setting headers automagically without your knowledge for instance).
As to the patch itself, what I really don't like about it, is that you create specific options that only make sense for the xhr transport.
If you really want those right now and insist on using ajax for low-level control over the xhr, you can easily copy the code of https://github.com/jquery/jquery/blob/master/src/ajax/xhr.js, apply your patch and change line 52 into:
Drop the file as is in your application: it'll override the default xhr transport and you'll be able to do whatever you need to.
That being said, I know we need some progress notification system but, guess what? We'll have progress callbacks on Deferreds/Promises in 1.7 which would be a perfect fit for this kind of things :)
Given Deferreds' progress is transport agnostic, each transport will be able to provide its own progress notification protocol.
I leave this as opened for now, given that I'd love to see something to notify readyState changes using Deferreds' progress make it as soon as 1.7. But, since this wasn't brought when proposals for new features in 1.7 were in order, I doubt we'd see this before 1.8. On the other hand, the addition would be pretty neutral since progress callbacks didn't exist earlier (no code breakage since existing behaviour stays unchanged).
Anyway:
I'll try and take this conversation forward in the next jQuery meeting.