AJAX動態刷新時間

**

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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章