Gregg Lind
Gregg Lind

Reputation: 21260

What is an Ajax call: 'success' or 'failure'?

This seems 101-level, but I can't find an answer! (instead I find links to what to do on success or failures, jQuery's ajax().success and the like.

My hunch is:

Upvotes: 3

Views: 1115

Answers (2)

Uku Loskit
Uku Loskit

Reputation: 42040

This is not really an AJAX, but jQuery question, because the success callback is only characteristic of jQuery, not AJAX on the whole.

From the jQuery source you can see how it is implemented

// If successful, handle type chaining
            if ( status >= 200 && status < 300 || status === 304 ) {

                // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
                if ( s.ifModified ) {

                    if ( ( lastModified = jqXHR.getResponseHeader( "Last-Modified" ) ) ) {
                        jQuery.lastModified[ ifModifiedKey ] = lastModified;
                    }
                    if ( ( etag = jqXHR.getResponseHeader( "Etag" ) ) ) {
                        jQuery.etag[ ifModifiedKey ] = etag;
                    }
                }

                // If not modified
                if ( status === 304 ) {

                    statusText = "notmodified";
                    isSuccess = true;

...
else fail with the error callback

So, basically if the HTTP response code is between 200 and 299 (inclusive) or 304 it goes for the success callback, otherwise, it is the error callback.

Upvotes: 7

Evert
Evert

Reputation: 8541

an ajax call returns 1 of 4 different readystates (4 is the important one where data is received)

as well as the status, which can be any of of the normal response statuses (200 is no errors)

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

a useful example:

http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first

Upvotes: 1

Related Questions