Ajax调用成功是失败的

本文关键字:失败 成功 调用 Ajax | 更新日期: 2023-09-27 18:08:22

这个ajax调用从未创建成功警报,即使它已经到达并返回了服务器端方法上的success = true。

@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 });
        }

Ajax调用成功是失败的

你在服务器上得到一个异常-试着调试。net代码,或者用浏览器工具观察你的响应,看看它。

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

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

编辑

实际上,如果您在服务器上调试,看起来您无法看到它-您必须在响应中看到它。显然,异常在Json方法之后被进一步抛出。