user1679941
user1679941

Reputation:

What are the return parameters from jQuery .ajax with dataType json?

I have the following:

    var row = 99;
    $.ajax({
        cache: false,
        url: "/Admin/" + obj.table + "s/JsonUpdate",
        dataType: 'json',
        type: 'POST',
        data: { PartitionKey: pk,
                RowKey: rk,
                Entity: entity,
                Field: type, 
                Value: val }
    })
    .done(updateFieldDone(json, textStatus, XMLHttpRequest, row))
    .fail(function (jqXHR, textStatus, errorThrown) {
        ajaxOnFailure(jqXHR, textStatus, errorThrown)
    });

But I am confused about what exactly is returned with .done. Is it okay to code updateFieldDone(json, textStatus, XMLHttpRequest, row) like this. Previously i just had updateFieldDone() but the problem I have is that I need to pass a parameter called row. How can I do that?

Upvotes: 2

Views: 626

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

You could use the context parameter to pass additional data to the success callback:

var row = 99;
$.ajax({
    cache: false,
    url: "/Admin/" + obj.table + "s/JsonUpdate",
    dataType: 'json',
    type: 'POST',
    context: { row: row },
    data: { 
        PartitionKey: pk,
        RowKey: rk,
        Entity: entity,
        Field: type, 
        Value: val 
    }
})
.done(function(json) {
    // Here "this" will represent the object we passed as context
    var row = this.row;
})
.fail(function (jqXHR, textStatus, errorThrown) {
    ajaxOnFailure(jqXHR, textStatus, errorThrown)
});

or if you want it in a separate function:

var row = 99;
$.ajax({
    cache: false,
    url: "/Admin/" + obj.table + "s/JsonUpdate",
    dataType: 'json',
    type: 'POST',
    context: { row: row },
    data: { 
        PartitionKey: pk,
        RowKey: rk,
        Entity: entity,
        Field: type, 
        Value: val 
    }
})
.done(updateFieldDone)
.fail(function (jqXHR, textStatus, errorThrown) {
    ajaxOnFailure(jqXHR, textStatus, errorThrown)
});

and then define the function:

function updateFieldDone(json) {
    // Here "this" will represent the object we passed as context
    var row = this.row;
}

Upvotes: 2

Related Questions