**
AJAX動態刷新時間
**
Controllers
[WebMethod]
public JsonResult SayHello()
{
return Json(DateTime.Now.ToString(), JsonRequestBehavior.DenyGet);
}
前臺頁面代碼
<p id="ttt">
ajax刷新時間:
<script>
//500毫秒調用一次Show()
setInterval("Show()", 500);
</script>
</p>
<script>
function Show() {
var $t = document.getElementById("ttt"); // 得到容器對象
$.ajax({
//要用post方式
type: "Post",
//方法所在頁面和方法名
url: "/Home/SayHello",
//contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//返回的數據用data.d獲取內容
$t.innerHTML = JSON.stringify(data);
},
error: function (err) {
$t.innerHTML = "出錯了";
}
});
}
</script>