2013-04-10 149 views
1

即使ajax调用已达到服务器端方法并返回成功= true,该调用也不会在成功时创建警报。Ajax调用成功失败

@model IEnumerable<Test.Models.Task> 
@Styles.Render("~/Content/Site.css") 
@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 

    <div id ="alerts"> 

      @Html.Action("_Tasks") 
      <script type="text/javascript"> 
       $(document).ready(function poll() { 

        $.ajax({      
         type: 'GET', 
         cache: false, 
         url: '@Url.Action("TasksRefresh")', 
         dataType: "json", 
         complete: function() { setTimeout(poll, 10000); },      
         success: function (data) { 
          alert("Testing") 

         } 
        }); 
       })(); 
     </script> 

     @* <script type="text/javascript"> 
      var alerts = '@ViewBag.Alerts'; 
      @foreach (var i in alerts) 
      { 

      } 
     </script>*@ 

    </div> 
<table> 
    <tr> 
     <th>Category</th> 
     <th>Severity</th> 
     <th>Assigned to Role</th> 
     <th>Assigned To</th> 
     <th>Chart #</th> 
     <th>Note</th> 
     <th>Alert</th> 
     <th>Status</th> 
     <th>Creator By</th> 
     <th>Create Date</th> 
     <th>Due Date</th> 



     <th></th> 

    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.LookupTaskCategory.CategoryName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.LookupTaskSeverity.SeverityName) 
     </td>  

     <td> 
      @Html.DisplayFor(modelItem => item.AssignedToRoleName) 
     </td> 

     <td> 
      @Html.DisplayFor(modelItem => item.AssignedToName) 
     </td> 

     <td> 
      @Html.DisplayFor(modelItem => item.Patient.ChartNo) 
     </td> 

     <td> 
      @Html.DisplayFor(modelItem => item.Note) 
     </td> 

     <td> 
      @Html.DisplayFor(modelItem => item.AlertFlag) 
     </td> 

     <td> 
      @Html.DisplayFor(modelItem => item.LookupTaskStatu.StatusName) 
     </td> 

     <td> 
      @Html.DisplayFor(modelItem => item.CreatedByName) 
     </td> 

     <td> 
      @Html.DisplayFor(modelItem => item.CreatedOnDate) 
     </td> 

     <td> 
      @Html.DisplayFor(modelItem => item.DueDate) 
     </td> 

     <td> 
      @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | 
      @Html.ActionLink("Details", "Details", new { id=item.Id }) | 
      @Html.ActionLink("Delete", "Delete", new { id=item.Id }) 
     </td> 
    </tr> 
} 

</table> 

这是我的控制器中的服务器端方法。我试图用ActionResult替换JsonResult,但它并没有改变结果。

public JsonResult TasksRefresh() 
     { 
      //Testing to see if this return ever gets received by ajax. 
      return Json(new { success = true }); 
     } 
+1

'$(document).ready()'块后的'()'不正确。你不能从'.ready()'中调用返回值,因为它不是函数。这应该会导致错误 - 您应该**始终**在处理客户端代码时打开错误控制台! – Pointy 2013-04-10 14:36:02

+0

如何在Visual Studio中获取错误控制台以查看ajax错误?你的解决方案是不正确的btw。 – zms6445 2013-04-10 14:40:49

+0

这不是一个解决方案。您不使用Visual Studio查看我正在谈论的错误。这是客户端错误,因此您需要使用浏览器的调试工具。 – Pointy 2013-04-10 14:44:00

回答

3

您在服务器上发生异常 - 尝试调试.NET代码或查看您的浏览器工具的响应,以查看它。

如果你想在GET方法返回一个JSON对象,你需要包括一个JsonRequestBehavior参数为Json调用,如:

return Json(new { success = true }, JsonRequestBehavior.AllowGet); 

编辑

事实上,它看起来像如果您在服务器上进行调试,则无法看到它 - 您必须在响应中看到它。显然,在Json方法之后,例外情况会进一步恶化。

+0

没有问题。如果你有兴趣,[这里](http://haacked.com/archive/2009/06/25/json-hijacking.aspx)是它的背后的安全目的 - 有点时髦的漏洞,但它是有道理的。但是这引起了我很多次 - 我现在只是在我所有的AJAX调用上发布POST,所以我不会忘记这一点。 – 2013-04-10 14:43:33