2013-04-10 147 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