AJAX获取服务器当前时间

------------------------------ WebService1.asmx----------------------------------

// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。

    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    
        [WebMethod]
        public string GetDate()
        {
            return DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
        }

    }

------------------------------------HTMLPage1.htm---------------------------------------

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
     <script src="js/Jquery1.7.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
           function GetDate() {
               $.ajax({
                   type: "post", //客户端向服务器发送请求时采取的方式
                   contentType: "application/json", //指定客户端发送给服务器的内容的类型以及服务器返回给客户端内容的类型为json格式
                  url: "WebService1.asmx/GetDate", //指明客户端要向哪个页面里面的哪个方法发送请求
                   data: "{}", //指定伴随发送的请求传递到服务器的参数
                   success: function (result) {//客户端调用服务器端方法成功后执行的回调函数。
                      $('#mydiv').text(result.d);
                   }
              })
          }
            setInterval(GetDate, 1000);
        })
    </script>
</head>
<body>
<div id="mydiv"></div>
    <input id="Button1" type="button" value="获取服务器时间" />
</body>
</html>


原文地址:https://www.cnblogs.com/javawebsoa/p/3089202.html