Last Updated: February 25, 2016
·
515
· oleggavrilov

Simplify ajax query results testing

$.get("api/loadAll")
.done(function(){ alert("$.get succeeded"); })
.fail(function(){ alert("$.get failed!"); });

Availible in jQuery >1.5. You can use any function in place of $.get. For my datacontext, I use something like this:

// Ajax helper
function ajaxRequest(type, url, data) { 
    var options = {
        dataType: "json",
        contentType: "application/json",
        cache: false,
        type: type,
        data: ko.toJSON(data)
    };
    return $.ajax(url, options);
}
// making a call
ajaxRequest("post", url, postData)
        .done(function (result) {
            // show user some love
        })
        .fail(function () {
            // and dont get mad
        });