Ajax Request Timeout

465 views
Skip to first unread message

Rick

unread,
Jul 13, 2009, 5:34:15 AM7/13/09
to Prototype & script.aculo.us
I have googled for Ajax Request Timeout, but i didn't finde a good
working solution.

These both are for most people the best solutions.

http://positionabsolute.net/blog/2008/07/prototype-ajax-request-timeout.php
http://codejanitor.com/wp/2006/03/23/ajax-timeouts-with-prototype/

But these solutions just work half, it worked under IE8 but not under
FF3

I tried to fix it my self, so i fixed the problem that under firefox,
always the onSuccess comes, even if the server is
down.

---- prototype.js line: 1494 ------------------------------
success: function() {
var status = this.getStatus();
return !status || (status >= 200 && status < 300);
<---------------- maby the problem
},

---- prototype.js line: 1494 changed ------------------
success: function() {
var status = this.getStatus();
return (status >= 200 && status < 300 && status != 0); <--------
works got a onFailure event, if server is down
},
------------------------------------------------------------------------

after this fix i have a correct onFailure event in FF, IE and Opera.

T.J. Crowder

unread,
Jul 13, 2009, 11:25:09 AM7/13/09
to Prototype & script.aculo.us
Hi,

I don't know why status 0 is considered "success" in Prototype (this
has been noted before[1]). I'll be interested in whether a core dev
chimes in with an explanation for that. As Doug Reeder points out in
the link above, status 0 happens on some browsers when the 'net
connection is down.

But leaving aside status 0, just a logical point. The third condition
in your updated code:

> return (status >= 200 && status < 300 && status != 0); <--------

...is irrelevant and will never be evaluated. If 200 <= x < 300, x
cannot be zero. To remove the part about status 0 being success, just
remove it, you don't have to tack anything further on:

return (status >= 200 && status < 300);

[1] http://proto-scripty.wikidot.com/prototype:how-to-bulletproof-ajax-requests

FWIW,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available

On Jul 13, 10:34 am, Rick <dun...@googlemail.com> wrote:
> I have googled for Ajax Request Timeout, but i didn't finde a good
> working solution.
>
> These both are for most people the best solutions.
>
> http://positionabsolute.net/blog/2008/07/prototype-ajax-request-timeo...http://codejanitor.com/wp/2006/03/23/ajax-timeouts-with-prototype/

Matt Foster

unread,
Jul 13, 2009, 4:56:22 PM7/13/09
to Prototype & script.aculo.us
Hey Rick,

I wrote the positionabsolute article on this topic. I double
checked the code. I had patched the success method as well in my
scenario(look for red syntax in the code block), but it does seem to
be working for me in both FF3 and IE7. If you'd like to collaborate
further on this idea I'd be more than happy to discuss it with you and
work towards actually getting a committed patch for prototype
incorporated into core.

Check out the demo page and tell me if you're still having trouble in
IE.

http://positionabsolute.net/projects/javascript/AjaxTimeout/


Also i've been working on a more standalone project that works with
prototype but isn't totally reliant on it for ajax communications, in
the end i want to achieve timeout, caching and history support all
under one roof.

http://positionabsolute.net/blog/2009/03/agile-ajax.php


Thanks,
Matt




On Jul 13, 11:25 am, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
> Hi,
>
> I don't know why status 0 is considered "success" in Prototype (this
> has been noted before[1]).  I'll be interested in whether a core dev
> chimes in with an explanation for that.  As Doug Reeder points out in
> the link above, status 0 happens on some browsers when the 'net
> connection is down.
>
> But leaving aside status 0, just a logical point.  The third condition
> in your updated code:
>
> >     return (status >= 200 && status < 300 && status != 0); <--------
>
> ...is irrelevant and will never be evaluated.  If 200 <= x < 300, x
> cannot be zero.  To remove the part about status 0 being success, just
> remove it, you don't have to tack anything further on:
>
>     return (status >= 200 && status < 300);
>
> [1]http://proto-scripty.wikidot.com/prototype:how-to-bulletproof-ajax-re...
>
> FWIW,
> --
> T.J. Crowder
> tj / crowder software / com
> Independent Software Engineer, consulting services available
>
> On Jul 13, 10:34 am, Rick <dun...@googlemail.com> wrote:
>
> > I have googled for Ajax Request Timeout, but i didn't finde a good
> > working solution.
>
> > These both are for most people the best solutions.
>
> >http://positionabsolute.net/blog/2008/07/prototype-ajax-request-timeo...
>

Matt Foster

unread,
Jul 13, 2009, 4:59:12 PM7/13/09
to Prototype & script.aculo.us
update on that, i didn't highlight it in red but patched it all the
same.

Came to the same conclusion TJ did, just removed the preceding !
status...

success: function() {
var status = this.getStatus();
return (status >= 200 && status < 300);
},


On Jul 13, 11:25 am, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
> Hi,
>
> I don't know why status 0 is considered "success" in Prototype (this
> has been noted before[1]).  I'll be interested in whether a core dev
> chimes in with an explanation for that.  As Doug Reeder points out in
> the link above, status 0 happens on some browsers when the 'net
> connection is down.
>
> But leaving aside status 0, just a logical point.  The third condition
> in your updated code:
>
> >     return (status >= 200 && status < 300 && status != 0); <--------
>
> ...is irrelevant and will never be evaluated.  If 200 <= x < 300, x
> cannot be zero.  To remove the part about status 0 being success, just
> remove it, you don't have to tack anything further on:
>
>     return (status >= 200 && status < 300);
>
> [1]http://proto-scripty.wikidot.com/prototype:how-to-bulletproof-ajax-re...
>
> FWIW,
> --
> T.J. Crowder
> tj / crowder software / com
> Independent Software Engineer, consulting services available
>
> On Jul 13, 10:34 am, Rick <dun...@googlemail.com> wrote:
>
> > I have googled for Ajax Request Timeout, but i didn't finde a good
> > working solution.
>
> > These both are for most people the best solutions.
>
> >http://positionabsolute.net/blog/2008/07/prototype-ajax-request-timeo...
>

Rick

unread,
Jul 14, 2009, 1:57:16 PM7/14/09
to Prototype & script.aculo.us
hi

> But leaving aside status 0, just a logical point.  The third condition
> in your updated code:
>
> >     return (status >= 200 && status < 300 && status != 0); <--------
>
> ...is irrelevant and will never be evaluated.  If 200 <= x < 300, x
> cannot be zero.

i can't believe what piece rubbish of code i have written, my brain
must have bin offline :'(


i think the zero comes from that part:
------- line: 1499--------------------------------------
getStatus: function() {
try {
return this.transport.status || 0;
} catch (e) { return 0 }
}
-----------------------------------------------------------

so Firefox must leave the status undefined.


@ Matt Foster : i tried your patch but it didn't work for me,
but maybe its my fault ... maybe it was a bad day of coding for me :)

i will try it again .... yes the Ajax part off prototype is a bit
uncomfortable


by the way.... sorry I am not a native English speaker, so don't be
mad with me ;)

Matt Foster

unread,
Jul 14, 2009, 2:20:51 PM7/14/09
to Prototype & script.aculo.us
Hey Rick,

My proposed idea in that article requires you to actually
redefine the Ajax.Request. If we can solidify the idea we could
submit the code as a patch to core but as of now you've gotta either
replace it in the source library itself or just redefine it after the
library but before any implementation.

Review that demo page to see how I had done it and you could probably
get it working.

Rick

unread,
Jul 14, 2009, 3:49:46 PM7/14/09
to Prototype & script.aculo.us
your patch didn't work for me ....

in ff i didn't get the onTimeout event .... or if you get then it
happend when the server is back online
an the as often as it should happend while the offline time.

Rick

unread,
Jul 14, 2009, 3:56:13 PM7/14/09
to Prototype & script.aculo.us
currently i just fixed the success function

success: function() {
var status = this.getStatus();
return (status >= 200 && status < 300);
},

and just watch the onFailure and onException event.

But i would like to have an onTimeout event, so if we get an solution
for a crossbrowser working onTimeout event, we can submit it.
I have read about an solution but this one was rejected because it
didn't worked in safari, etc.

Matt Foster

unread,
Jul 14, 2009, 5:52:31 PM7/14/09
to Prototype & script.aculo.us
Yeah I discuss the idea of having its own event for timing out in my
article. The argument is a timeout is a failure, its failed to
retrieve a response in the given time. So a timeout is basically like
an inherited event from onFailure. Question remains do you seperate
the event or just add a flag to the XHR to indicate the reason for
failure is its timed out. Obviously you side with the latter as did
Tobie so perhaps it would be best to dispatch both events for ease of
implementation.

Also that success method looks exactly like what TJ suggested and I
posted, so good going on that.

Rick

unread,
Jul 15, 2009, 9:03:59 AM7/15/09
to Prototype & script.aculo.us
@Matt Foster: your patch didn't work for me ....??

in ff i didn't get the onTimeout event .... or if you get then it
happend when the server is back online
an the as often as it should happend while the offline time.

i don't know why?

i use an PeriodicalExecuter for my ajax request ...

that's how i do it without timeout:
------my apps
code-------------------------------------------------------
new PeriodicalExecuter(
function (pe) {

var successListener = function(transport, json){
appendPost('posts', transport.responseJSON);
scrollToEnd();

//########FireBUG###################
console.log("Event : ","AJAX : Request successed");
//########FireBUG###################
};

var failureListener = function(transport, json){
show_alert('Fehler beim Aufbau der Verbindung zum Server.');

//########FireBUG###################
console.log("Event : ","AJAX : Request failed");
//########FireBUG###################
};

var exceptionListener = function(transport, json){
show_alert('Es liegt eine unbekannter Fehler vor.');

//########FireBUG###################
console.error("Event : ","AJAX : Request exception happend");
//########FireBUG###################
};

new Ajax.Request('app.php', {
method:'post',
parameters: { controller: 'check' },
onSuccess: successListener,
onException: exceptionListener,
onFailure: failureListener
});

}, 4);
-----------------------------------------------------------

i've used your patch an added an onTimeout event an the other stuff,
but under Firefox this event just happend when the server is back
online
under InternetExplorer it worked fine???!

Matt Foster

unread,
Jul 15, 2009, 5:01:49 PM7/15/09
to Prototype & script.aculo.us
> @Matt Foster: your patch didn't work for me ....??

Yes you've said this now 3 times... Unfortunately for you, the patched
Ajax.Request along with my implementation works fine in both browsers,
so somewhere along the line you've misconfigured things.

Looking at the code you've just posted, you're executing
Ajax.Request's in a PE loop, you just keep sending them out in
intervals?

The idea of a "timeout" is to say, if this request doesn't finish in
30 seconds, then abort the request.

If you're looking to detect whether the user has network connectivity
(ie for Gears) then i'd suggest a different approach...

http://positionabsolute.net/blog/2007/09/google-gears-offline-detection.php
Reply all
Reply to author
Forward
0 new messages