Domanda

Sto cercando di implementare la tecnica della cometa in streaming XHR in jQuery, ma sto avendo problemi a rilevare l'evento onreadystatechange. Sto usando Firefox 4.0.1.

Sto usando Prefilters (disponibile da jQuery 1.5) per modificare il nativo Oggetto XHR, in particolare per aggiungere un gestore di eventi all'evento onreadystatechange. Ho un'implementazione grezzo di quello http://jsfiddle.net/rdx6f/ . Quel codice aggiunge al documento gli attributi generacodicitagCodegCodeGcode e readyState dell'oggetto XHR quando viene licenziato l'evento responseText. Stranamente, tuttavia, sembra solo rilevare l'evento onreadystatechange quando onreadystatechange è 1.

In altre parole, JQuery sembra rilevare l'evento generacodicitagcode quando l'oggetto XMLHTTPRequest è in stato "aperto" (readtState 1), ma non quando l'oggetto XHR è in stato "Intestazioni ricevute" (ReadyState 2), "Caricamento 2)" "(ReadyState 3), né" Fatto "(ReadyState 4).

Stranamente, se I readyState nel gestore di eventi, quindi I DO Vedere la gestione onreadystatechange quando ReadyState è 1, 2, 3 e 4. Vedi http://jsfiddle.net/rdx6f/1/ . Stesso codice come http://jsfiddle.net/rdx6f/ , solo alert()ing invece di aggiungerli al documento. < / P >.

Posso anche rilevare eventi onreadystatechange quando alert() è 1, 2, 3 o 4 in JavaScript nativo - vedere http: / /jsfiddle.net/d7vah/ . (Quel codice viene preso quasi verbatim dall'attuazione di Dylan Schiemann di Streaming XHR a pagina 115 del libro "Siti web ancora più veloci".)

Sto semplicemente facendo qualcosa di sbagliato qui, come usare i prefilters Ajax JQuery sbagliati o facendo un errore JavaScript? Questo è forse un bug in jquery?

È stato utile?

Soluzione

FYI: I looked into this quite deeply and it turns out that jQuery (since 1.5) uses the onreadystatechange event internally. The consequence of this is that jQuery will always write it's internal function to the onreadystatechange event, overwriting whatever you set it to. This happens whenever the AJAX call is actually sent, which means the function you wrote for onreadystatechange will work until that time (which means it will work until readyState = 1, but not longer).

This is the proposed workaround at the moment: http://jsfiddle.net/tBTW2/

At the moment, I cannot see a different (better) workaround without either not using jQuery (or going back to 1.4.4 or lower version, where it works perfectly) or editing the jQuery code (because as it is right now they will always overwrite whatever you try to hook in).

However, in upcoming versions (most likely in 1.8), the jQuery team are planning on implementing support for triggering events depending on the different readyStates. They will use Deferreds in some way.

This is the ticket that keeps this upcoming change:

http://bugs.jquery.com/ticket/9883

Altri suggerimenti

Here is the prefiltering done correctly linked from the jQuery bugzilla bug #8327,

http://jsfiddle.net/d8ckU/1/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top