ajax长链接--拉实现
生活随笔
收集整理的這篇文章主要介紹了
ajax长链接--拉实现
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? ? ?很多時(shí)候需要用在網(wǎng)頁(yè)上實(shí)時(shí)顯示數(shù)據(jù),這個(gè)時(shí)候一般要用到長(zhǎng)鏈接技術(shù)。最簡(jiǎn)單的實(shí)現(xiàn)就是ajax輪詢,也就是拉的方式。 下面是一個(gè)簡(jiǎn)單的例子:在網(wǎng)頁(yè)上實(shí)時(shí)顯示服務(wù)器時(shí)間。
后端WebService代碼:
/// <summary>/// ServerTime 的摘要說(shuō)明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class ServerTime : System.Web.Services.WebService
{
/// <summary>
/// 獲取服務(wù)器端時(shí)間
/// </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">
//輪詢刷新服務(wù)器時(shí)間
setInterval(showTime, 1000);
function showTime() {
callWebService("ServerTime.asmx/GetTime", null, function (data) {
$("#serverTime").html(data);
});
}
</script>
<div id="serverTime"></div>
</body>
</html>
轉(zhuǎn)載于:https://www.cnblogs.com/alala666888/archive/2012/02/13/2349499.html
總結(jié)
以上是生活随笔為你收集整理的ajax长链接--拉实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【转】SQL Server中行列转换 P
- 下一篇: Direct3D学习_绘制