.JQuery .ajax如何使用返回值调用WCF方法? [英] .JQuery .ajax how do I call WCF method with return value?

查看:70
本文介绍了.JQuery .ajax如何使用返回值调用WCF方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够使用.ajax调用WCF方法.如何调用返回值的方法?我需要调用此方法以查看数据是否准备就绪,如果还没有等待一秒钟. WCF方法是:

I have been able to call WCF methods with .ajax. How do I call a method that returns a value? I need to call this method to see if data is ready and if not wait one second. The WCF method is:

 [OperationContract]
        [WebGet]
        public bool IsDataReady(string requestGUID)
        {
            if (Global.publicDataDictionary.Keys.Contains(requestGUID))
                return true;
            else return false;
        }

到目前为止,我的JavaScript是:

My JavaScript so far is:

$(document).ready(function() {
            var input = {requestGUID:"<%=guid %>"};
            console.log(input);

            $.ajax({
                url: "http://www.blah.com/services/TestsService.svc/IsDataReady",
                type: "GET",
                contentType: "application/json; charset=utf-8",
                data: input,
                dataType: "json",
                success: function(data) {

                }
            });

我将第二个Ajax调用分解为一个方法,但是我的日志显示,对第二个Web服务的调用从不传递requestGUID.我不能使用相同的输入变量吗?

I broke out the 2nd ajax call into a method but my logging is showing that the call to 2nd web service never passes a requestGUID. Can't i use the same input variable?

   var guid = "<%= this.guid%>";
        // var input = '{"SbiId":"' + guid + '"}';
        // var input = {requestGUID:"ca222cf7-be5e-431a-ab93-9a31e8ae2f4a"};

        function callUpdateGrid(input) {
            console.log(input);
            $.ajax({
                url: "http://www.blah.com/services/TestsService.svc/GetContactsDataAndCountbyGUID",
                type: "GET",
                contentType: "application/json; charset=utf-8",
                data: input,
                dataType: "json",
                success: function (data) {
                    var mtv = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
                                console.log(data);
                                mtv.set_dataSource(data.d.Data);
                                mtv.dataBind();
                            }
            });
        }


        function CallIsDataReady(input) {
            $.ajax({
                url: "http://www.blah.com/services/TestsService.svc/IsDataReady",
                type: "GET",
                contentType: "application/json; charset=utf-8",
                data: input,
                dataType: "json",
                success: function (data) {
                    if (!data) {
                        setTimeout(function (inputInner) { CallIsDataReady(inputInner); }, 1000);
                    }
                    else {
                        //Continue as data is ready
                        callUpdateGrid(input);
                    }
                }
            });
        }

        $(document).ready(function () {
            var input = { requestGUID: "<%=guid %>" };

            CallIsDataReady(input);

        });

我将第二个Ajax调用分解为一个方法,但是我的日志显示,从未调用第二个Web服务的调用:

I broke out the 2nd ajax call into a method but my logging is showing that the call to 2nd web service is never getting called:

url: "http://www.blah.com/services/TestsService.svc/GetContactsDataAndCountbyGUID",

else {
                            //Continue as data is ready
                            callUpdateGrid(input);
                        }

推荐答案

返回值将包含在ajax调用中传递给成功回调设置的data参数中.

The return value will be contained in the data parameter passed to the success callback setup in your ajax call.

您将需要在此处检查该值,然后如果设置为false,则设置一个超时,该超时将在到期时再次尝试进行ajax调用.

You will need to check the value here, then if false, setup a timeout, which on expiry will attempt the ajax call again.

最好将IMO封装在一个函数中,以便您可以在超时后以递归方式调用Ajax调用.例如

It would be best IMO to wrap up the Ajax call inside a function, that you can call in a recursive fashion when the timeout has expired. e.g.

    function CallIsDataReady(input){
        $.ajax({
            url: "http://www.blah.com/services/TestsService.svc/IsDataReady",
            type: "GET",
            contentType: "application/json; charset=utf-8",
            data: input,
            dataType: "json",
            success: function(data) {
                if (!data){
                    setTimeout(function(){CallIsDataReady(input);}, 1000);
                }
                else{
                  //Continue as data is ready
                }
            }
        });
    }
    $(document).ready(function() {
        var input = {requestGUID:"<%=guid %>"};
        console.log(input);

        CallIsDataReady(input);
    });

这篇关于.JQuery .ajax如何使用返回值调用WCF方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆