Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top