我有一个问题,当我调用这个返回bool的函数时,我得到bool undefined。 它应该返回我bool值我试过var bool = funcationName()但它返回undeffiend
IsaAtionResultExist
createAction
$("#createAction").click(function() {
var bools = IsaAtionResultExist(Clinetfirstname, Clinetlastname, datasource, status, cliendid);
if (bools === true) {
return false;
}
});
}
});
IsaAtionResultExist 在警报中显示为
function IsaAtionResultExist(Clinetfirstname, Clinetlastname, datasource, status, cliendid) {
alert("Second");
debugger;
$.ajax({
type: "GET",
url: '@Url.Action("IsaAtionResultExist", "P4C")',
data: { Clinetfirstname: Clinetfirstname, Clinetlastname: Clinetlastname, datasource: datasource, status: status, cliendid: cliendid },
success: function (data) {
if (data === true) {
alert(data);
return data;
} else {
return false;
}
}
});
}
答案 0 :(得分:0)
您正在尝试从ajax回调中返回数据,这是不可能的。 Javascript是异步的,因此包装函数不会返回ajax回调返回。
答案 1 :(得分:0)
$.ajax(...)
是一个返回promise的异步调用,只能在promise解析后检查boolean。我已经更改了您的AJAX函数以返回该承诺,如下所示:
function IsaAtionResultExist(Clinetfirstname, Clinetlastname, datasource, status, cliendid) {
return $.ajax({
type: "GET",
url: '@Url.Action("IsaAtionResultExist", "P4C")',
data: {
Clinetfirstname: Clinetfirstname,
Clinetlastname: Clinetlastname,
datasource: datasource,
status: status,
cliendid: cliendid
},
success: function (data) {
if (data === true) {
alert(data);
return data;
} else {
return false;
}
}
});
}
然后在您的点击事件中,您需要执行以下操作:
$("#createAction").click(function() {
IsaAtionResultExist(Clinetfirstname, Clinetlastname, datasource, status, cliendid)
.done((bools) => {
// do something with your bools
// but not a return statement unless you need it for a successive
// callback chain
};
});