AJAX before jQuery 1.8 and After

Sam Deering
Tweet

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.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • 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?