【发布时间】:2020-07-26 16:52:34
【问题描述】:
我无法将通过 Jquery 中的 Ajax 获得的 Json 存储在全局变量中,它只能在函数内部工作。
let ingJson;
let masasJson;
let salsasJson;
searchIngredients();
console.log(ingJson);
当我需要将 Json 返回给我时,log 会向我抛出一个 undefined 错误,以便我可以在其余代码中使用它。
function searchIngredients(){
let origin = `/ingredientes.json`;
$.get(origin).then(htmlIngredients);
}
function htmlIngredients(res){
ingJson = res.ingredientes;
masasJson = res.masas;
salsasJson = res.salsas;
console.log(ingJson);
}
这里 log 正确返回 Json。
【问题讨论】: