XMLHttpRequest abort() and subsequent error in call to onreadystatechange  
Author Message
Peter Michaux





PostPosted: 2006-10-28 12:51:27 Top

javascript, XMLHttpRequest abort() and subsequent error in call to onreadystatechange Hi,

I am trying out the Ajax timeout example in Flanagan's book p492. It
works fine in Opera but in Firefox the the call to request.abort()
doesn't stop onreadystatechange being called. I found some references
to this problem in the group archives and on google but no solutions I
can seem to implement here. The troublesome code is below. I added what
I think are fixes for the IE memory leak probem in two places (which
doesn't change this problem) and the third argument to open().

After the timeout function runs I see

[Exception... "Component returned failure code: 0x80040111
(NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult:
"0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame ::
http://localhost:3000/flanaganTimeout.html :: anonymous :: line 68"
data: no]

Where line 68 is the line "if (request.status == 200) {". How can I
stop onreadystatechange from being called?

HTTP.get = function(url, callback, options) {
var request = HTTP.newRequest();

var timer;
if (options.timeout) {
timer = setTimeout(function() {
request.abort();
if (options.timeoutHandler) {
options.timeoutHandler(url);
}
request = null; // IE leak
},
options.timeout);
}

request.onreadystatechange = function() {
if (request.readyState == 4) {
if (timer) {clearTimeout(timer);}
if (request.status == 200) {
callback();
}
request = null; // IE leak
}
}
request.open("GET", url, true); // added third argument
request.send(null);
return;
};


Thank you,
Peter

 
ASM





PostPosted: 2006-10-28 18:06:00 Top

javascript >> XMLHttpRequest abort() and subsequent error in call to onreadystatechange Peter Michaux a 閏rit :
> Hi,
>
> I am trying out the Ajax timeout example in Flanagan's book p492. It
> works fine in Opera but in Firefox the the call to request.abort()
> doesn't stop onreadystatechange being called. I found some references
> to this problem in the group archives and on google but no solutions I
> can seem to implement here. The troublesome code is below. I added what
> I think are fixes for the IE memory leak probem in two places (which
> doesn't change this problem) and the third argument to open().
>
> After the timeout function runs I see
>
> [Exception... "Component returned failure code: 0x80040111
> (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult:
> "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame ::
> http://localhost:3000/flanaganTimeout.html :: anonymous :: line 68"
> data: no]

what did you give as argument 'callback' when calling HTTP.get() ?


> Where line 68 is the line "if (request.status == 200) {". How can I
> stop onreadystatechange from being called?
>
> HTTP.get = function(url, callback, options) {
> var request = HTTP.newRequest();
 
Peter Michaux





PostPosted: 2006-10-28 23:08:00 Top

javascript >> XMLHttpRequest abort() and subsequent error in call to onreadystatechange ASM wrote:
> Peter Michaux a 閏rit :
> > Hi,
> >
> > I am trying out the Ajax timeout example in Flanagan's book p492. It
> > works fine in Opera but in Firefox the the call to request.abort()
> > doesn't stop onreadystatechange being called. I found some references
> > to this problem in the group archives and on google but no solutions I
> > can seem to implement here. The troublesome code is below. I added what
> > I think are fixes for the IE memory leak probem in two places (which
> > doesn't change this problem) and the third argument to open().
> >
> > After the timeout function runs I see
> >
> > [Exception... "Component returned failure code: 0x80040111
> > (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult:
> > "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame ::
> > http://localhost:3000/flanaganTimeout.html :: anonymous :: line 68"
> > data: no]
>
> what did you give as argument 'callback' when calling HTTP.get() ?

Here is the full call.

HTTP.get('/ajax/sleep_long',
function(){console.log('server responded');},
{timeout:2000,
timeoutHandler:function(){console.log('timeout');}}
);

I'm using firebug in Firefox with this test so console.log() works much
like alert() but without stalling everything. I tried with alert() for
Opera and Firefox. Works in Opera but doesn't work in Firefox.

Thank you,
Peter