运行ajax请求时遇到问题,在 Safari 和 Chrome 中,而不是在Firefox中
var doStuff = function(){
callLoadingScreen();
jsRoutes.controllers.SomeController.someServerSideMethod().ajax({
//ajax stuff
});
closeLoadingScreen();
}
在safari和chrome中,当调用doStuff()时,会跳过callLoadingScreen()方法,但只有在ajax请求完成后才会发送ajax请求,而不是加载callLoadingScreen()方法。
在firefox中,一切都按原样运行,但不是在chrome和safari中。这是一些游戏问题,还是一个jquery问题。我已经尝试了很多修复,但似乎没有任何工作。我有一些我失踪的东西,我之前做过这种加载,但不记得有这个问题。
欢迎任何帮助或反馈。谢谢。
答案 0 :(得分:2)
假设您的代码未运行使用async: false
的ajax调用(您绝不应该这样做),它应该在任何浏览器上都不能正常运行。< / p>
在调用closeLoadingScreen
之前,您需要等待Ajax调用完成:
e.g。
var doStuff = function(){
callLoadingScreen();
jsRoutes.controllers.SomeController.someServerSideMethod().ajax({
//ajax stuff
complete: function(){
closeLoadingScreen();
}
});
}
在您的示例中,它正在执行此操作:
callLoadingScreen();
closeLoadingScreen();
// Get response from Async Ajax call here sometime much later!
如果您想使用promises,请尝试这种方式:
var doStuff = function(){
callLoadingScreen();
jsRoutes.controllers.SomeController.someServerSideMethod().ajax({
//ajax stuff
}).then(closeLoadingScreen);
}