【问题标题】:asp.net call WebMethod from Javascript asyncronousasp.net 从 Javascript 异步调用 Web 方法
【发布时间】:2011-11-01 18:01:28
【问题描述】:

我正在尝试构建一个每秒更新一些状态文本的 asp.net(c#) 页面。 现在我已经实现了一个按钮,它调用另一个 PageMethod 重新启动某些东西并需要一点时间。问题是,当我调用重新启动 PageMethod 时,只要重新启动方法正在进行,更新 PageMethod 就无法更新...

我写了一个小例子来说明我的意思:

我的页面中的 WebMethods:

    [WebMethod]
    public static string Update()
    {
        //return "a" to see when the Update PageMethod succeeded
        return "a";
    }

    [WebMethod]
    public static string Restart()
    {
        //the restart will take a while
        Thread.Sleep(2000);
        //return "a" to see when the Restart PageMethod succeeded
        return "a";
    }

要更新的 html 元素:

<p id="update" style="float:left;"></p>
<p id="restart" style="float:right;"></p>

Page 方法调用:

callUpdate()
            function callUpdate() {
                PageMethods.Update(function (text) {
                    //itself+text from pagemethod
                    $('#update').text($('#update').text() + text);
                });
                setTimeout(callUpdate, 1000);
            }

            callRestart()
            function callRestart() {
                PageMethods.Restart(function (text) {
                    //itself+text from pagemethod
                    $('#restart').text($('#restart').text() + text);
                });

                setTimeout(callRestart, 1000);
            }

注意:更新完成后也会每秒调用一次,只是为了看看它是如何工作的

澄清一下:我希望 PageMethods 的执行独立于另一个 PageMethod 已经完成。

我还浏览了一些链接,例如: http://esskar.wordpress.com/2009/06/30/implementing-iasyncresult-aka-namedpipeclientstream-beginconnect/

http://msdn.microsoft.com/en-us/library/aa480516.aspx

但我认为这不是我需要的 (?) 而且我真的不知道如何从 Javascript(BeginXXX 和 Endxxx)调用它

*编辑:*

关于 Massimiliano Peluso,js 代码如下所示:

        callUpdate()
        function callUpdate() {
            $.ajax({
                type: "POST",
                url: "ServicePage.aspx/Update",
                data: "{}",
                contentType:
                "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $('#update').text($('#update').text() + msg.d);
                }
            });
            setTimeout(callUpdate, 1000);
        }

        callRestart()
        function callRestart() {
            $.ajax({
                type: "POST",
                url: "ServicePage.aspx/Restart",
                data: "{}",
                contentType:
                "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $('#restart').text($('#restart').text() + msg.d);
                }
            });
            setTimeout(callRestart, 1000);
        }

注意:当我用新的js运行Page时,出现的问题和之前一模一样:Update方法什么也做不了,直到Restart方法结束。

【问题讨论】:

    标签: c# jquery asp.net webmethod


    【解决方案1】:

    您应该改为使用异步调用来调用页面方法。

    看看下面。这是一种使用JQuery调用页面方法的通用方式

    $.ajax({
      type: "POST",
      url: "PageName.aspx/MethodName",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        // Do something interesting here.
      }
    });
    

    您应该使用此代码来替换页面方法调用

    PageMethods.Restart(function (text))
    
    PageMethods.Update(function (text))
    

    【讨论】:

    • msg 是 JSON 对象吗?我尝试了var jsonObj = $.parseJSON(msg); alert(jsonObj); 之类的方法,但收到了 null 的警报
    • msg.d 已经做到了,d 是因为 .NET Framework 的 xss 防护。
    【解决方案2】:

    这是因为只有在没有其他请求正在处理时才会执行请求。 那是因为两个进程无法访问同一个SessionState(会话状态不是线程安全的)。

    因此,要同时处理请求,您必须将@Page 指令中的EnableSessionState 设置为'ReadOnly' 或'false'

    【讨论】:

      猜你喜欢
      • 2015-04-10
      • 2015-06-04
      • 2017-11-23
      • 2015-05-12
      • 2020-03-10
      • 2017-02-23
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      相关资源
      最近更新 更多