【发布时间】:2016-06-09 18:23:22
【问题描述】:
我正在尝试验证对数据库的两次调用中的一个字段。它进入数据库并验证它的真假。我需要链接一些 AJAX 调用来执行此操作。我正在使用.when、.then 和.done 来执行此操作,但它似乎不起作用。
var Validate = function () {
var isValid = true;
$errorList.find('li').remove();
$lblAError.text('');
$.when(ParcelValidate(isValid))
.then(AccountValidate(isValid))
.done(function () {
return isValid
});
};
var ParcelValidate = function (isValid) {
return $.ajax({
url: "../WebServices/ParcelMasterWebService.asmx/IsParcelActive",
method: "POST",
data: JSON.stringify({ "pin": $parcID.val() }),
contentType: 'application/json; charset=utf-8',
datatype: 'json',
success: function (data) {
if (!data.d) {
isValid = false;
$lblPError.text('!').css(({ "color": "red" }));
$errorList.append('<li>Parcel must be on record.</li>').css(({ "color": "red" }));
}
},
fail: function () {
isValid = false;
$lblPError.text('!').css(({ "color": "red" }));
$errorList.append('<li>Unexpected error occured!</li>').css(({ "color": "red" }));
}
})
}
var AccountValidate = function (isValid) {
return $.ajax({
url: "../WebServices/FireProtectMasterWebService.asmx/isAccountActive",
method: "POST",
data: JSON.stringify({ "accountID": $parcID.val() }),
contentType: 'application/json; charset=utf-8',
datatype: 'json',
success: function (data) {
if (data.d) {
isValid = false;
$lblPError.text('!').css(({ "color": "red" }));
$errorList.append('<li>Cannot have duplicate Parcels.</li>').css(({ "color": "red" }));
}
},
fail: function () {
isValid = false;
$lblPError.text('!').css(({ "color": "red" }));
$errorList.append('<li>Unexpected error occured!</li>').css(({ "color": "red" }));
}
})
}
【问题讨论】:
-
你会定义“似乎没有工作吗?”您期望会发生什么,会发生什么,以及您尝试过什么来解决它?您看到了哪些错误消息?
-
我需要它来运行一个 ajax 调用然后下一个。只有在那些完成并运行成功或失败部分之后,我才希望它返回 isValid 给我。现在它开始了 ajax 调用,然后继续前进并返回 undefined,然后调用又回来了,没有任何东西可以返回。