感谢@phil h的帮助,我用承诺解决了找不到的错误。
function getDataById(url, cache) {
return new Promise(function(resolve, reject){
$.ajax({
method: "GET",
url: url,
success: function (data) {
console.log("ajax data", data);
console.log("cache", cache);
cache[url] = data;
resolve(data)
},
error:function(err){
reject(err);
}
});
});
}
在服务器调用中
else {
console.log("loading from server");
fn(url, cache).then(function(response){
console.log("response", response);
changeTitle(response);
});