var alertStatus = false;
$.ajax({
type : "GET",
url : url,
dataType : "json",
async: false,
data : {},
success : function(data) {
console.log("success");
var undef = undefined;
if(typeof(data.error) != "undefined"){
layer.msg(alertMessage.error);
}else{
alertStatus = true;
}
},
error : function(jqXHR) {
layer.msg("网络连接错误");
console.log("发生错误:" + jqXHR.status);
}
});
console.log("begin");
return alertStatus;
async: false 使ajax同步执行,即当ajax中的方法执行完成后才会继续执行;当async: true时ajax下面的语句不等到ajax中的方法执行完后就会执行,在控制台打印时会发现ajax下的打印语句会早于success中的打印语句执行
本文探讨了使用jQuery发起同步Ajax请求的细节,展示了如何通过设置async属性为false来确保Ajax请求完全执行后再继续执行后续代码。文章包括了示例代码,解释了在控制台中打印语句的执行顺序,并讨论了在不同async设置下打印语句的出现时机。
3519

被折叠的 条评论
为什么被折叠?



