我想驗證兩個調用到數據庫的字段。它會轉到數據庫並驗證它的真或假。我需要鏈接幾個AJAX調用來做到這一點。我正在使用.when
,.then
和.done
來做到這一點,但它似乎沒有工作。jQuery當然後完成
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調用,然後下一個。只有在這些完成並運行成功或失敗部分後,我是否希望它返回給我。現在它開始ajax調用,然後繼續並返回undefined,然後調用返回並且沒有任何返回。 – Shane