Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax request. Which callback is executed first complete or success?

I could spike this to find out, but I'm going to use SO. In my unit tests (qunit) I use the asynchShould (alias for asynchTest) test. Part of the assertion is to wait for the completion/success of the request. Like this:

asyncShould('talk to customer list server', 1, function() {
    stop(2000);
    var forCustomerList = newCustomerListRequest();

    forCustomerList.page = 'helpers/helper.php';
    forCustomerList.data += '&action=customerListServer&DB=11001';
    var originalSuccess = forCustomerList.success;

    forCustomerList.success = function(msg) {
        if (msg.flash !== undefined && msg.data !== undefined && msg.status !== undefined) {
            ok(true, 'json structure correct')
        }
        else {
            ok(false, 'json structure not correct');
        }
        originalSuccess(msg);
        start();
    };

    testController.getServerData(forCustomerList);
})
like image 429
Gutzofter Avatar asked May 29 '10 17:05

Gutzofter


People also ask

How do I know if AJAX request is successful?

The ajaxStop() method specifies a function to run when ALL AJAX requests have completed. When an AJAX request completes, jQuery checks if there are any more AJAX requests. The function specified with the ajaxStop() method will run if no other requests are pending.

What is difference between success and complete in AJAX?

success() only gets called if your webserver responds with a 200 OK HTTP header - basically when everything is fine. However, . complete() will always get called no matter if the ajax call was successful or not - maybe it outputted errors and returned an error - . complete() will still get called.

How would you fire a callback when an AJAX request on a page has completed?

The ajaxComplete() method specifies a function to be run when an AJAX request completes. Note: As of jQuery version 1.8, this method should only be attached to document. Unlike ajaxSuccess(), functions specified with the ajaxComplete() method will run when the request is completed, even it is not successful.

How does AJAX success work?

AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.


2 Answers

success

From the jQuery site http://api.jquery.com/jQuery.ajax/

complete(XMLHttpRequest, textStatus)Function

A function to be called when the request finishes (after success and error callbacks are executed)...

like image 152
Luca Filosofi Avatar answered Sep 27 '22 21:09

Luca Filosofi


"complete" fires after "success" or "failure"

like image 25
zed_0xff Avatar answered Sep 27 '22 20:09

zed_0xff