views:

89

answers:

2

what is the correct way to determine that an AJAX call is successful?

I see in prototype.js

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

and in jQuery:

    // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
    return !xhr.status && location.protocol == "file:" ||
        ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223;

which one is correct? If we don't use any Javascript library but write the AJAX in basic Javascript, which one should we use?

+5  A: 

Prototype seems more 'correct' as it only treats valid HTTP success codes as success. jQuery is more robust as it takes account of bugs and other things that are frequently success codes.

workmad3
/seconded - I would not regard a 304 as successful for example, that should instead cause another cycle of behaviour before overall success can be established
annakata
both of them so well establish a javascript library... but differ in opinion of how it is to be successful
動靜能量
A: 

HTTP statuses fall into these categories:

2XX: success

3XX: redirect

4XX: client error

5XX: server error.

So 200-300 is an "ok" result for an ajax call.

for more details on status codes, check out http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

cheers,

jrh.

Here Be Wolves
304 is was not modified, so it is equally valid too, it seems
動靜能量