Javascript Ajax异步请求问题
在下面的代码中,当我使用alert()框时,按下alert框后,内容就会加载到页面上。但是,如果删除alert()框,则不会加载内容。只是想让你知道谁读了这篇文章,很抱歉,我已经阅读了很多关于这个“Ajax”请求的文章,但是,我无法理解这一点。有什么帮助吗Javascript Ajax异步请求问题,javascript,jquery,ajax,Javascript,Jquery,Ajax,在下面的代码中,当我使用alert()框时,按下alert框后,内容就会加载到页面上。但是,如果删除alert()框,则不会加载内容。只是想让你知道谁读了这篇文章,很抱歉,我已经阅读了很多关于这个“Ajax”请求的文章,但是,我无法理解这一点。有什么帮助吗 var myPage = myPage || {}; myPage.datas = ""; myPage.Content = (function(){ $.ajax({ async: true, type:'GET', url
var myPage = myPage || {};
myPage.datas = "";
myPage.Content = (function(){
$.ajax({
async: true,
type:'GET',
url: 'JSON/carousel-data.json',
dataType: "json",
success: function(data) {
myPage.datas = myPage.dataRetive(data);
}
});
})();
myPage.dataRetive = function(dataIn){
return dataIn;
}
alert(myPage.datas); // Here is the place, if i use this alert, then, content is loading, otherwise, it is not.
myPage.globals = {
Contact: $("#Contact")[0],
About : $("#About")[0],
Careers : $("#Careers")[0],
sampMaxLimits: 20,
Images: {
fpn1: myPage.datas[0].image, // error comes these place
fpn2: myPage.datas[1].image,
fpn3: myPage.datas[2].image
}
}
修改您的代码如下
$.ajax({
async: true,
type:'GET',
url: 'JSON/carousel-data.json',
dataType: "json",
success: function(data) {
myPage.datas = myPage.dataRetive(data);
myPage.globals = {
Contact: $("#Contact")[0],
About : $("#About")[0],
Careers : $("#Careers")[0],
sampMaxLimits: 20,
Images: {
fpn1: myPage.datas[0].image, // now myPage.datas will be available
fpn2: myPage.datas[1].image,
fpn3: myPage.datas[2].image
}
}
}
});
现在它将正确更新myPage.globals