如何将以下同步ajax调用转换为异步? [英] How to convert the below synchronous ajax call to asynchronous?
问题描述
Hello Experts,
我在用户表单中使用jquery验证用户的可用性。
我正在使用JQuery ValidationEngine。
Hello Experts,
I am validating the availability of user using jquery in user form.
I am using JQuery ValidationEngine.
_checkUserAvailability: function (field, rules, i, options) {
var result = "noError";
$.ajax({
type: "POST",
url: "RegistrationForm.aspx/checkUserAvailability",
data: JSON.stringify({ userID: field.attr('value') }),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (data, status) {
callError(data.d);
},
failure: function (data, status) {
},
error: function (data, status) {
}
});
function callError(data) {
if (data == false) {
result = "Error";
}
}
if (result == "Error") {
return options.allrules.checkUserAvailability.alertText;
}
}
如何将上述同步ajax调用转换为异步?
当我制作async:true代码时,代码不再有效。
我想让代码异步同时我返回options.allrules.checkUserAvailability.alertText; _checkUserAvailability功能。
谢谢。
How to convert the above synchronous ajax call to asynchronous?
When I make "async: true" the code is no more working.
I want to make the code asynchronous at the same time I return "options.allrules.checkUserAvailability.alertText;" to "_checkUserAvailability" function.
Thanks.
推荐答案
.ajax({
type : POST,
url: RegistrationForm.aspx / checkUserAvailability,
data:JSON.stringify({userID:field.attr(' value')}),
contentType: application / json; charset = utf-8,
dataType: json,
async : false ,
成功:函数(数据,状态){
callError(data.d);
},
fa ilure:function(data,status){
},
error:function(data,status){
}
});
函数callError(数据){
如果(data == false ){
result = 错误;
}
}
if (result == 错误){
return options.allrules.checkUserAvailability.alertText;
}
}
.ajax({ type: "POST", url: "RegistrationForm.aspx/checkUserAvailability", data: JSON.stringify({ userID: field.attr('value') }), contentType: "application/json; charset=utf-8", dataType: "json", async: false, success: function (data, status) { callError(data.d); }, failure: function (data, status) { }, error: function (data, status) { } }); function callError(data) { if (data == false) { result = "Error"; } } if (result == "Error") { return options.allrules.checkUserAvailability.alertText; } }
如何将上述同步ajax调用转换为异步?
当我制作async:true代码时,代码不再有效。
我想让代码异步同时我返回options.allrules.checkUserAvailability.alertText; _checkUserAvailability功能。
谢谢。
How to convert the above synchronous ajax call to asynchronous?
When I make "async: true" the code is no more working.
I want to make the code asynchronous at the same time I return "options.allrules.checkUserAvailability.alertText;" to "_checkUserAvailability" function.
Thanks.
尝试下面的一个。
注意: 1.当您删除async:false时,它会自动变为异步功能。
2.You无法在成功方法之外测试异步结果。所以你必须在回调函数中检查你的结果(成功)。
Try below one.
Note: 1.When you remove the async : false,it automatically becomes the async function.
2.You cannot test async result outside the success method.So you have to check your result inside the callback function (success).
_checkUserAvailability: function (field, rules, i, options) {
var result = "noError";
.ajax({
type:POST,
url :RegistrationForm.aspx / checkUserAvailability,
data:JSON.stringify({userID:field.attr('value')}),
contentType:application / json; charset = utf-8 ,
dataType:json,
成功:函数(数据,状态){
callError(data.d);
if(result ==Error ){
return options.allrules.checkUserAvailability.alertText;
}
},
失败:函数(数据,状态){
},
错误:函数(数据,状态) {
}
});
函数callError(data){
if(data == false){
result =Error;
}
}
}
.ajax({ type: "POST", url: "RegistrationForm.aspx/checkUserAvailability", data: JSON.stringify({ userID: field.attr('value') }), contentType: "application/json; charset=utf-8", dataType: "json", success: function (data, status) { callError(data.d); if (result == "Error") { return options.allrules.checkUserAvailability.alertText; } }, failure: function (data, status) { }, error: function (data, status) { } }); function callError(data) { if (data == false) { result = "Error"; } } }
这篇关于如何将以下同步ajax调用转换为异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!