zoukankan      html  css  js  c++  java
  • 用WebService实现web页面的局部刷新

    服务器的回传会引发整个页面的刷新,怎么样只让它刷新局部呢,前几天看见书上有讲用

    WebService行为的,今天试了试,效果还不错

    在web目录中需要一个文件webservice.htc

    可以到这里下载: http://msdn.microsoft.com/workshop/author/webservice/webservice.htc

    见一个WebService 文件名为Service1.asmx:

    [WebService(Namespace="http://localhost/BehaviorService/")]
    public class TestService : System.Web.Services.WebService
    {

    /// <summary>
    /// 返回服务器的时间
    /// </summary>
    [WebMethod]
    public string GetServerTime()
    {
    return DateTime.Now.ToString();
    }
    }

    在同级目录下进一个html文件,内容如下:

    <script>
    var intCallId = 0;

    function Init()
    {
    GetServerTime();
    setInterval("GetServerTime()",1000);
    }

    function GetServerTime()
    {
    Service.useService("Service1.asmx?WSDL","TestService");
    intCallId = Service.TestService.callService("GetServerTime");
    }

    function service_result()
    {
    if (event.result.error)
    {
    showresult.innerText = event.result.errorDetail.string;
    }
    else
    {
    showresult.innerText = event.result.value;
    }
    }

    </script>
    <html>
    <body onload="Init();">
    <div id="Service" style="behavior:url(webservice.htc)" onresult="service_result()"></div>
    <span id=showresult></span>
    </body>
    </html>


    我查看http://localhost/BehaviorService/test1.htm就可以看到一个服务器上的时间了

    而且是会动的,通过这种方法就可以实现页面的局部刷新了

    有两点限制:

    1.行为只能用域内的web服务,因为DHTML内置的安全限制造成的

    2.通过的这种方式的访问的类型会被限制,行为支持.net的基本类型和它们的数组

    如:dataset,datatable得复杂类型就不支持了

  • 相关阅读:
    Nginx反向代理Mysql
    Postgresql数据迁移
    Docker安装及配置
    jstack用法
    Centos7系统添加Windows字体
    Bash美化
    ERROR: new encoding (UTF8) is incompatible xxx
    Python selenium 自动化脚本打包成一个exe文件(转载 原文https://www.jb51.net/article/178430.htm)
    python -m pip install --upgrade pip 失败
    Warning: no saslprep library specified. Passwords will not be sanitized
  • 原文地址:https://www.cnblogs.com/weiweictgu/p/354018.html
Copyright © 2011-2022 走看看