我在 ajax.php 页面中放置了 sleep(5) 。我需要返回的代码来启动另一个功能组。它也是使用ajax。我的第一个 ajax 调用如下所示:
$.ajax({
url: '/ajax.php',
data: {
id : code
} ,
type: 'POST',
async:false, //<<< here
cache: false,
beforeSend: function(){
$('#loading').dialog();
},
success: function(data){
console.log(data.result);
$('#loading').dialog('close');
initAnotherFunctionGrop(data.result);
},
error: function(){
$('#loading').dialog('close');
}
});
为什么我在 IE 和 Chrome 中无法显示加载消息?只有 Firefox 正在处理这个问题。
最佳答案
异步代码是最好的。同步代码可能会挂起您的浏览器,这对于 ajax 来说是一个坏主意,因为 ajax 请求的速度取决于用户计算机和浏览器之外的因素。您不希望用户的计算机挂起,因此请避免这种情况。相反,尝试这样的事情。
function a(passedData){
return $.ajax({
url : '/ajax.php',
data : passedData
});
}
function b(passedData){
return $.ajax({
url : '/ajaxB.php',
data : passedData
});
}
$.when(a(data),b(data)).then(function(successDataForA,successDataForB){
//Do code after all asynchronous ajax calls are done.
//As a whole this is still asynchronous so other things can still run
},function(failA,failB){
//This fail callback is not necessary but here it is if needed
});
关于javascript - Ajax 与 jQuery 同步。是否可以?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12256335/