繁体   English   中英

asp.net从Javascript asyncronous调用WebMethod

[英]asp.net call WebMethod from Javascript asyncronous

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

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

Web页面中的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>

Pagemethod调用:

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方法完成之前无法执行任何操作。

你应该使用异步调用来调用页面方法。

看看下面的内容。 这是使用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))

这是因为请求仅在没有其他请求正在处理时继续进行。 那是因为两个进程无法访问同一个SessionState (Sessionstate不是Threadsafe)。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM