2015-12-02 73 views
0

我想以5秒的间隔刷新页面,该页面应该使用来自SQL Server的最新数据更新视图。以下是代码。使用AJAX和JQuery以设定的时间间隔自动刷新页面

@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-

+0

变量结果是什么包含 – gaetanoM

+0

请尝试在'success'中添加'console.log(result)'并告诉我们输出是什么。 –

+0

那么,http:// localhost:28886对于URL来说不是太有用。也许你把它放在网上的某个地方?或者甚至更好地在这里创建一个可运行的代码。 –

回答

-1
  1. 你有你的JS语法错误。我认为你的JS代码末尾缺少括号。
  2. Ajax不会刷新页面 - 您只需获取您当然可以放置在DOM上的内容,或者可以随意操作它。
  3. 指向您本地主机服务器的URL当然不起作用 - 必须公开。
+0

#3是不正确当然,这样的#1,#2,他已经在这样做。 –