ajax长链接--拉实现
生活随笔
收集整理的這篇文章主要介紹了
ajax长链接--拉实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ?很多時候需要用在網頁上實時顯示數據,這個時候一般要用到長鏈接技術。最簡單的實現就是ajax輪詢,也就是拉的方式。 下面是一個簡單的例子:在網頁上實時顯示服務器時間。
后端WebService代碼:
/// <summary>/// ServerTime 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class ServerTime : System.Web.Services.WebService
{
/// <summary>
/// 獲取服務器端時間
/// </summary>
/// <returns></returns>
[WebMethod]
[ScriptMethod(UseHttpGet=true, ResponseFormat=ResponseFormat.Json)]
public string GetTime()
{
return DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss");
}
}
前段html代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.js" ></script>
<script type="text/javascript">
function callWebService(url, param, handler) {
$.ajax({
url: url,
data: param,
type: "get",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (p) {
handler(p.d);
}
});
};
</script>
</head>
<body>
<script language="javascript" type="text/javascript">
//輪詢刷新服務器時間
setInterval(showTime, 1000);
function showTime() {
callWebService("ServerTime.asmx/GetTime", null, function (data) {
$("#serverTime").html(data);
});
}
</script>
<div id="serverTime"></div>
</body>
</html>
轉載于:https://www.cnblogs.com/alala666888/archive/2012/02/13/2349499.html
總結
以上是生活随笔為你收集整理的ajax长链接--拉实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】SQL Server中行列转换 P
- 下一篇: Direct3D学习_绘制