我正在尝试接收多个 JSON 文件的数据。我有我需要的数据,但是多次,即使我使用了 return 语句。我需要getData()函数中的数据。这需要进展到loadImage()功能。我不知道为什么代码多次返回相同的内容。提前致谢。我的 Chrome 开发者控制台:我的目录结构:JSON文件结构:{ "filename": "legs", "fileExtension": "png", "changesIfJumping": true, "customizeable": true}
1 回答

陪伴而非守候
TA贡献1757条经验 获得超8个赞
使用更现代的方式-获取,而不是XHR
async function getData(characterPart) {
try {
let response = await fetch(`./character/${characterPart}/data.json`);
return response.json()
} catch (e) {
throw new Error(`Could not get file with the name "data.json" in the directory "${characterPart}".`);
}
}
// TEST
async function start() {
window.defaultData = await getData('xxx');
}
start();
添加回答
举报
0/150
提交
取消