Javascript 建立Ajax承诺
我有一个功能,可以向使用Ajax和C#web服务的数据库中添加一条记录。在更新DB之前,我调用另一个函数来验证同样使用Ajax的输入。因此,在继续添加记录之前,我需要完成validate函数 我知道,由于ajax的异步特性,我不得不使用promise/deferred,但我无法正确设置它 已更新Javascript 建立Ajax承诺,javascript,c#,jquery,ajax,promise,Javascript,C#,Jquery,Ajax,Promise,我有一个功能,可以向使用Ajax和C#web服务的数据库中添加一条记录。在更新DB之前,我调用另一个函数来验证同样使用Ajax的输入。因此,在继续添加记录之前,我需要完成validate函数 我知道,由于ajax的异步特性,我不得不使用promise/deferred,但我无法正确设置它 已更新 function validate() { var deferred = $.Deferred(); $.ajax({ url: "path/to/web/se
function validate() {
var deferred = $.Deferred();
$.ajax({
url: "path/to/web/service",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: params,
}).done(function (result) {debugger
if (!result || result.d === "") {
isValid = false;
}
else {
var duplicate = JSON.parse(result.d);
switch (duplicate) {
case DuplicateData.Name:
isValid = false;
break;
case DuplicateData.ID:
isValid = false;
break;
}
}
}).fail(function (jqXHR, textStatus, errorThrown) {
alert(textStatus + ' - ' + errorThrown + '\n' + jqXHR.responseText);
});
deferred.resolve(isValid);
return deferred.promise();
//return isValid;
}
$(document).on("click", "#btnAdd", function (event) {debugger
$.when(validate())
.then(function(isValid) {
if (isValid) {
$.ajax({
url: "path/to/another/webservice",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: some_param,
}).done(function (result) {
addNewRecord();
)}.fail(function (jqXHR, textStatus, errorThrown) {
alert(textStatus + ' - ' + errorThrown + '\n' + jqXHR.responseText);
});
}
})
});
function addNewRecord(){
// add record to DB
}
由于您只处理布尔结果,因此没有理由返回值,您只需
解析
或拒绝
这回答了你的问题吗?我更新了问题,加入了承诺,但它仍然执行按钮添加功能,然后进入验证功能,然后添加新记录,即使验证失败,也很高兴尝试添加新记录。谢谢。不幸的是,我无法在IE中测试这一点,因为在函数validate()中理解“=>”太愚蠢了,更不幸的是,IE是标准的公司浏览器@幸运的是,
(resolve,reject)=>
只是编写函数(resolve,reject)
的一种奇特方式。但IE被称为“Internet Explorer”?可以说是上升到11的那个?这甚至都不支持承诺,你必须履行它。。。或者继续使用延迟
,但不使用.promise()
@NoBullMan好的,我应用了更改,但现在是我无法测试。。。希望它能起作用,谢谢。当我从大师那里学到新东西的时候总是很棒的。
function validate() {
var $defer = $.Deferred(function() {
$.ajax({
url: "path/to/web/service",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: params,
})
.done(function (result) {
// If there's no result
if (!result || !result.d) {
$defer.reject();
}
else {
// This call shouldn't be necessary, as dataType: "json" will call it for you
// Unless you double-encoded it.
var duplicate = JSON.parse(result.d);
// Not sure about this part, what's DuplicatedData and what does result.d contain?
switch (duplicate) {
case DuplicateData.Name:
case DuplicateData.ID:
$defer.reject();
}
}
// Everything checks out, resolve the promise
$defer.resolve();
})
.fail(function (jqXHR, textStatus, errorThrown) {
// If there was a network or server error
// alert(textStatus + ' - ' + errorThrown + '\n' + jqXHR.responseText);
$defer.reject();
});
});
return $defer;
}
$('form').on("click", "#btnAdd", function (event) {
validate()
.done(function() {
// If the validation passes
$.ajax({
url: "path/to/another/webservice",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: some_param,
})
.done(function (result) {
// Creation successful
addNewRecord();
}).fail(function (jqXHR, textStatus, errorThrown) {
// Creation failed
alert(textStatus + ' - ' + errorThrown + '\n' + jqXHR.responseText);
});
})
.fail(function(reason) {
// Called in case of a validation error
console.log("Validation error");
});
});