------------------------------ 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---------------------------------------
$(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);
})