JavaScript传递函数参数与返回值一起输出

时间:2015-07-10 12:42:17

标签: javascript jquery

如何在Java Script中传递ajax get的函数参数以及返回值?

在下面的示例中,我想通过GetServer函数参数id的值来使其可以访问函数returnValue

function getServerList(id) {
        $.ajax({
            type: "GET",
            url: "/BackEndFunction/" + id,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: returnValue
        });
}

function returnValue(Data) {
    var _size = 0;
    var id= id // passed from getserverlist
    for (var i = 0; i< Data.length; i++) {
        _size += Data[i]._size;
    }
    data_dictionary[id] = _size;
}

2 个答案:

答案 0 :(得分:3)

救援的匿名功能

function getServerList(id) {
    $.ajax({
        type: "GET",
        url: "/BackEndFunction/" + id,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            returnValue(data, id);
        }
    });
}

答案 1 :(得分:1)

您也可以代理它

function getServerList(id) {
    $.ajax({
        type: "GET",
        url: "/BackEndFunction/" + id,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: $.proxy(returnValue, this, id)
    });
}