使用AJAX和JQuery以设置的间隔自动刷新页面

Automatically refreshing a page at set interval using AJAX and JQuery

本文关键字:刷新 AJAX JQuery 设置 使用      更新时间:2023-09-26

我尝试每隔5秒刷新一个页面,该页面应该会使用SQL Server的最新数据更新视图。下面是代码。

@model Test.Data.Domain.ManufacturingCdMachine
@{
    ViewBag.Title = "Rimage Details";
    ViewBag.JobId = Model.CurrentManufacturingJobId;
}
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top" id="navbar">
        <div class="container">
            <div class="navbar-header">
            </div>
        </div>
    </div>
    <div id="CdMachineDetails">
        @if (@Model.CurrentManufacturingJobId != null)
        {
            <div>
                @{Html.RenderPartial("_CdMachineJob");}
            </div>
        }
        else
        {
            <div>
                @{Html.RenderPartial("_MachineInIdle");}
            </div>
        }
    </div>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
</body>
</html>
<script type="text/javascript"
        src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
    $(document).ready(
            function() {
                setInterval(function() {
                    var randomnumber = Math.floor(Math.random() * 100);
                    $.ajax({
                        type: "GET",
                        url: "/Mobile/CdMachine/Details/" + @Model.ManufacturingCdMachineId,
                        data: {
                        },
                        success: function (result) {
                            //Sets your content into your div
                            $('#CdMachineDetails').html(result);
                        }
                    });
                }, 5000);
            });
</script>

我的网页的URL是http://localhost:28886/Mobile/CdMachine/Details/1

然而,这个页面似乎并不令人耳目一新。我做错了什么?

-Alan-

  1. 你的JS中有语法错误。我认为您的JS代码末尾缺少括号
  2. Ajax不刷新页面——您只需要获得内容,当然您可以将这些内容放在DOM上,也可以根据需要对其进行操作
  3. 指向本地主机服务器的URL当然不起作用,必须公开