基于Json函数的javascript函数返回值
我的JS代码如下所示:基于Json函数的javascript函数返回值,javascript,asp.net,json,validation,Javascript,Asp.net,Json,Validation,我的JS代码如下所示: function ValidateBid(source, args) { var txtValue = $('#txtBid').val(); $.ajax({ type: "POST", async: true, url: "BidDetail.aspx/ValidateValue", co
function ValidateBid(source, args) {
var txtValue = $('#txtBid').val();
$.ajax({
type: "POST",
async: true,
url: "BidDetail.aspx/ValidateValue",
contentType: "application/json; charset=utf-8",
data: "{value:" + JSON.stringify(txtValue) + "}",
dataType: "json",
success: function (msg) {
var resultAsJson = msg.d // your return result is JS array
args.IsValid = msg.d;
// Now you can loop over the array to get each object
}
});
return args.IsValid;
}
这里发生的是,它首先返回args.IsValid,然后进入args.IsValid方法。我使用了CustomValidator,并在其ClientValidationFunction上调用此函数
我想要的是,我想要根据Json函数ValidateValue返回的值返回true或false。请求是异步的,因此它将在设置之前返回值。因此,我认为如果您更改async:false将起作用。不可能返回从该调用所在的函数的异步调用中获得的值;您的函数将在异步调用返回之前结束 所以…使用回调:
function callback(result) {
// do womething with that result
}
function ValidateBid(source, args, callback) {
// do ajax stuff
// when you get the result from your ajax call do:
callback(result);
}