修改传递给.done()的ajax响应

时间:2018-05-06 16:37:28

标签: ajax jquery-deferred

this.getFullAddress(id).done(function(data) {
    // need to have both the response (data) and the id
});

getFullAddress(id) {
    var response = $.ajax({
        url: 'http://whatever.com'
    }); // modify this (add id)

    return response;
}

有谁知道如何做到这一点?

1 个答案:

答案 0 :(得分:0)

好的,找到了:

getFullAddress(id) {
    $.ajaxSetup({
        dataFilter: function (response) {
            response = JSON.parse(response);
            response['id'] = id;
            response = JSON.stringify(response);

            return response;
        }
    });

    return $.ajax({
        url: 'http://whatever.com'
    });
}