我似乎无法获取此值以将isValid值从以下代码段中的ajax调用中传回:
function isShortUrlAvailable(sender, args) {
var isValid = false;
$.ajax({
type: "POST",
url: "/App_Services/ShortUrlService.asmx/IsUrlAvailable",
data: "{url: '" + args.Value + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
isValid = response.d;
},
error: function (msg) {
isValid = false;
}
});
args.IsValid = isValid;
}
我确信它只是一些关于我正在忽略的闭合的简单方法。有人可以帮忙吗?
它用于asp.net自定义验证器。
这就是发生的事情:
答案 0 :(得分:1)
AJAX方法异步意味着您将值设置为false
,AJAX调用已启动,但是当它发生时,args.IsValid
行被调用。只需删除变量并在每个方案中设置args.IsValid
值:
function isShortUrlAvailable(sender, args) {
$.ajax({
type: "POST",
url: "/App_Services/ShortUrlService.asmx/IsUrlAvailable",
data: "{url: '" + args.Value + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
args.IsValid = response.d;
},
error: function (msg) {
args.IsValid = false;
}
});
}