ajax同步执行async:false无效的解决方法
无效的情况:
function ManHourCheck() {
var StartDate = $("#StartDate").val();//日报日期
var EndDate = $("#EndDate").val();//完成日期
var UserID = $("#UserID").val();//员工ID
$.ajax({
async: false,//加了这一行也没用!!!!!!!!!!!!!!!
type: "POST",
url: "/Daily/ManHourCheck",
data: {
"StartDate": StartDate,
"EndDate": EndDate,
"UserID": UserID,
},
success: function (result) {
if (result.success) {
return true;//取不到!!!!!!!!
}
else {
layer.alert(result.message, { area: ['500px', ''] });
return false;//也取不到!!!!!!!!
}
}
});
}
修改为:
function ManHourCheck() {
var StartDate = $("#StartDate").val();//日报日期
var EndDate = $("#EndDate").val();//完成日期
var UserID = $("#UserID").val();//员工ID
var check = false;//看这里!!!!!!!!!!!!!
$.ajax({
async: false,
type: "POST",
url: "/Daily/ManHourCheck",
data: {
"StartDate": StartDate,
"EndDate": EndDate,
"UserID": UserID,
},
success: function (result) {
if (result.success) {
check = true;//看这里!!!!!!!!!!!!!
}
else {
layer.alert(result.message, { area: ['500px', ''] });
check = false;//看这里!!!!!!!!!!!!!
}
}
});
//看这里!!!!!!!!!!!!!
if (check == true) {
return true;
}
else {
return false;
}
}
说明:
①不要在ajax的success里面做 return 值 !!!取不到的!!!
②用个变量去接 ajax的return 值, 然后 return 这个变量!!!就行了
③再不行的话,把ajax的return之后的操作的function写在success里.... 不推荐
其他参考一下这个: https://blog.csdn.net/qq_28938475/article/details/82800656