如何将参数传递给getJSON.then响应处理程序?

时间:2018-04-24 10:18:56

标签: javascript jquery jquery-deferred

我有一个对象数组,如果object.name属性与字符串匹配,我会循环并运行getJSON。我使用$ .then来处理响应。我想知道如何将额外的参数传递给$ .then中调用的响应函数?

//当前代码

var letters = [
    {name:"a", tag1:103, tag2:102},
    {name:"b", tag1:3},
    {name:"c", tag1:10},
    {name:"d", tag1:11, tag2:14},
    {name:"e", tag1:79, tag2:80},
    {name:"f", tag1:23, tag2:17},
];

function handleResponse(responseText) {
    console.log(responseText);
}

for (var i=0; i<letters.length; i++) {
    if (letters[i].name === "e") {
        $.getJSON(url).then(handleResponse);
    }
}

//我需要什么

function handleResponse(responseText, name) {
    console.log(name);
    console.log(responseText);
}

for (var i=0; i<letters.length; i++) {
    if (letters[i].name === "e") {
        $.getJSON(url).then(handleResponse(data, letters[i].name);
    }
}

0 个答案:

没有答案