即使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 });
}
'$(document).ready()'塊後的'()'不正確。你不能從'.ready()'中調用返回值,因爲它不是函數。這應該會導致錯誤 - 您應該**始終**在處理客戶端代碼時打開錯誤控制檯! – Pointy 2013-04-10 14:36:02
如何在Visual Studio中獲取錯誤控制檯以查看ajax錯誤?你的解決方案是不正確的btw。 – zms6445 2013-04-10 14:40:49
這不是一個解決方案。您不使用Visual Studio查看我正在談論的錯誤。這是客戶端錯誤,因此您需要使用瀏覽器的調試工具。 – Pointy 2013-04-10 14:44:00