Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于Json函数的javascript函数返回值_Javascript_Asp.net_Json_Validation - Fatal编程技术网

基于Json函数的javascript函数返回值

基于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

我的JS代码如下所示:

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);
}