【发布时间】:2014-10-22 19:17:35
【问题描述】:
我在 javascript 上找不到任何东西,但这可能会很快得到澄清。我正在一个网站上工作,我必须通过来自服务器的 http 请求检索数据。因为我需要发出几个请求并且数据是不变的,所以我想做的是用键和值制作表 -> 将这些值存储在文件中 -> 然后能够检索这些值。这样我必须读取一个文件,而不是通过 30 个 http 请求获取数据
总体思路:
Given: spell id = number (Ex. 45)
Output: name of spell = string (Ex. fire...)
Use this output to then fetch the url of the image of the spell (containing the spell name)
编辑
代码:
// When I fetch the file i use (json data)
function getChampionImage(id) {
var champUrl = "https://na.api.pvp.net/api/lol/static-data/na/v1.2/champion/" +id +"/?api_key=....."
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", champUrl, false );
xmlHttp.send( null );
var jsonText = xmlHttp.responseText;
var champData = JSON.parse(jsonText);
var champName = champData.key;
return "http://ddragon.leagueoflegends.com/cdn/" + versionNum + "/img/champion/"+champName+".png";
}
【问题讨论】:
-
如果您发布文件加载代码,我将向您展示如何集成缓存,非常简单。
-
我添加了获取 json 的代码,然后获取要在 url 中使用的数据。不知道你说的是不是这个
-
一个javascript对象是一个哈希表。
标签: javascript file-io hashmap