Tuesday, October 21, 2014

jQuery 1.8 update on ajax call

jQuery ajax call uses different syntax since version 1.8 [1].


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

Get html in jQuery before 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);
    }
});


The code above still works in 1.8. But the code below no longer works in 1.8:

$.post("get_news.php", { id: id }, function(data, status) {
    if (status == "success") {
        if ( data != '' ) {
            o.innerHTML = data;
        }
        return 1;
    } else {
        return 1; // ok
    }
}, 5);


Get html in jQuery since 1.8:

// cache: false is used to fetch the latest version
$.ajax({

    type: 'POST',
    url: "test.html",

    data: { id: id },
    dataType: 'html',
    cache: false
})
.done(function(data, textStatus, jqXHR)
{
    console.log(data);
})
.fail(function(jqXHR, textStatus, errorThrown)
{
    console.log('error: '+textStatus);
});



[1] http://www.sitepoint.com/ajax-jquery-1-8/

No comments:

Blog Archive

Followers