首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用Ajax导出函数返回值

问调用Ajax导出函数返回值
EN

Stack Overflow用户
提问于 2019-12-26 09:03:18
回答 2查看 226关注 0票数 0

当我在调用函数时返回

代码语言:javascript
复制
export function getChampion(id, country) {
    $.ajax({ 
        type: "GET",
        url: `http://ddragon.leagueoflegends.com/cdn/9.24.2/data/${country}/champion.json`,
        dataType: "json",
        success: function(data) {
            console.log(data);
            idToChampion(data, id);
        }
    });
}

// Return the ChampionId
function idToChampion(data, theId) {
    let resultObject = search(theId, data);
    console.log(resultObject);
    return resultObject.id;
}

function search(key, inputArray) {
    inputArray = Object.values(inputArray.data)
    for (let i = 0; i < inputArray.length; i++) {
        if (inputArray[i].key === key) {
            return inputArray[i];
        }
    }
}

函数"getChampion“返回undefined,我不知道为什么以及如何修复它。提前感谢

EN

回答 2

Stack Overflow用户

发布于 2019-12-26 09:49:46

如果你不想让它返回undefined,你可以使用Promise使它成为一个异步函数。

代码语言:javascript
复制
export function getChampion(id, country) {
    return new Promise(resolve => {
        $.ajax({ 
            type: "GET",
            url: `http://ddragon.leagueoflegends.com/cdn/9.24.2/data/${country}/champion.json`,
            dataType: "json",
            success: function(data) {
                resolve(data)
            }
        });
    })
}

然后你就可以使用

代码语言:javascript
复制
let data = await getChampion(id, country);
...
票数 0
EN

Stack Overflow用户

发布于 2019-12-26 10:04:40

您的getChampion函数没有返回任何值。

假设你想根据他们的id获取冠军数据,你可以尝试这个函数:

代码语言:javascript
复制
async function getChampion({id, lang = "en_US"}) {
  const {data} = await $.getJSON(`http://ddragon.leagueoflegends.com/cdn/9.24.2/data/${lang}/champion.json`).promise()
  return data[id]
}

下面是如何调用它:

代码语言:javascript
复制
(async () => {
  console.log(await getChampion("Aatrox"))
  //=> { "title": "the Darkin Blade", "blurb": "Once honored defenders of Shurima against the Void, Aatrox and his brethren would eventually become an even greater threat to Runeterra, and were defeated only by cunning mortal sorcery. But after centuries of imprisonment, Aatrox was the first to find...", ... }

  console.log(await getChampion("Ahri"))
  //=> { "title": "the Nine-Tailed Fox", "blurb": "Innately connected to the latent power of Runeterra, Ahri is a vastaya who can reshape magic into orbs of raw energy. She revels in toying with her prey by manipulating their emotions before devouring their life essence. Despite her predatory nature...", ... }
})()

您还可以使用promise接口:

代码语言:javascript
复制
getChampion("Aatrox").then(console.log)
//=> { "title": "the Darkin Blade", "blurb": "Once honored defenders of Shurima against the Void, Aatrox and his brethren would eventually become an even greater threat to Runeterra, and were defeated only by cunning mortal sorcery. But after centuries of imprisonment, Aatrox was the first to find...", ... }

getChampion("Ahri").then(console.log)
//=> { "title": "the Nine-Tailed Fox", "blurb": "Innately connected to the latent power of Runeterra, Ahri is a vastaya who can reshape magic into orbs of raw energy. She revels in toying with her prey by manipulating their emotions before devouring their life essence. Despite her predatory nature...", ... }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59482947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档