Here is a quick note of AJAX before jQuery 1.8 and the newer version. Don’t forget .success() and .error() still supported in jQuery 1.9.1 so it won’t break your old code and plugins you use. I have also drawn up some some New jQuery.ajax() Examples jQuery 1.9+ so check them out!

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

Get HTML using AJAX before jQuery 1.8

$.ajax({
    url: 'test.html',
    dataType: 'html',
    success: function (data, textStatus, xhr)
    {
        console.log(data);
    },
    error: function (xhr, textStatus, errorThrown)
    {
        console.log('error: '+textStatus);
    }
});

Get HTML using AJAX jQuery 1.8+

// cache: false is used to fetch the latest version

$.ajax({
    url: "test.html",
    cache: false
})
.done(function(data, textStatus, jqXHR)
{
    console.log(data);
})
.fail(function(jqXHR, textStatus, errorThrown)
{
    console.log('error: '+textStatus);
});

Mutiple callbacks can be specified for an $.ajax() request. Callback methods .done(), fail(), always(), then.() are all promise methods of the jqXHR object. All these callback methods fire once the $.ajax() process terminates. Promise callbacks are invoked in the order they are registered.

Sam Deering Sam Deering
Sam Deering is a Front-end Web Developer who specialises in JavaScript & jQuery. Sam is driven and passionate about sharing his knowledge to educate others.

  • http://profiles.google.com/gmilby Greg Milby

    i still do not see .done/.fail used very much
    tbh, i’m not 100% sure there’s an advantage(?)
    maybe there is?

Related books & courses
Available now on SitePoint Premium

Preview for $1