Skip to main content

Bug Tracker

Side navigation

#9324 closed bug (duplicate)

Opened May 17, 2011 10:46PM UTC

Closed July 23, 2011 01:32AM UTC

Last modified March 14, 2012 06:27AM UTC

Odd handling of 'onreadystatechange' event in jqXHR when readyState > 1

Reported by: anonymous Owned by:
Priority: low Milestone: 1.next
Component: ajax Version: 1.6.1
Keywords: Cc: jaubourg
Blocked by: Blocking:
Description

The doc at http://api.jquery.com/jQuery.ajaxPrefilter/ notes that prefilters give access to the jqXHR object (through which the native XHR should be accessible), but it seems like handlers attached to the XHR's onreadystatechange event act unexpectedly when readyState is > 1. http://bugs.jquery.com/ticket/8327 notes a jsfiddle providing a workaround to access some properties of the native XHR before readyState == 4, but the proposed solution seems inefficient: updates to the XHR's responseText are detected by polling that attribute every millisecond.

Instead, I think it would be better to simply add an event handler to the onreadystatechange event, and detect when readyState==3. I've tried doing that at http://jsfiddle.net/Rdx6f/, but the onreadystatechange event seems to only fire when readyState==1. (I'm using Firefox 4.0.1.) Oddly, when I alert the readyState upon onreadystatechange, things behave differently: http://jsfiddle.net/Rdx6f/1/.

A native JavaScript implementation of onreadystatechange tracking works as expected (http://jsfiddle.net/d7vaH/), making me think this is not a problem in Firefox 4.0.1.

A fix for this seeming bug would be useful -- e.g. for efficiently implementing XHR streaming.

Attachments (0)
Change History (6)

Changed May 18, 2011 07:17PM UTC by addyosmani comment:1

cc: → jaubourg
component: unfiledajax
priority: undecidedlow

Changed May 19, 2011 12:14PM UTC by jaubourg comment:2

onreadystatechange is used internally by the xhr transport, what you can do is something like this: http://jsfiddle.net/7JsUR/ using addEventListener.

Dunno how IE handles the situation though. Does attachEvent work on the xhr?

Changed May 19, 2011 01:08PM UTC by jaubourg comment:3

OK, that's the best cross-browser solution I can come up with that works in older IEs (8 and below): http://jsfiddle.net/tBTW2/

attachEvent doesn't work on the xhr which means that:

1) we cannot use it in the xhr transport (we have to use onreadystatechange)

2) we cannot use it in the prefilter

Note the use of "cache: false" in the options because IE is too quick to get the response (but in that case, do you need the progress info?)

Changed May 20, 2011 03:09PM UTC by dmethvin comment:4

status: newopen

Changed July 23, 2011 01:32AM UTC by jaubourg comment:5

resolution: → duplicate
status: openclosed

Changed July 23, 2011 01:32AM UTC by jaubourg comment:6

Duplicate of #9883.