c# - Ajax调用成功失败

标签 c# jquery ajax asp.net-mvc

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

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

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

最佳答案

您在服务器上遇到异常 - 尝试调试 .NET 代码,或使用浏览器工具观察您的响应来查看它。

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

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

编辑

实际上,如果您在服务器上调试,您似乎看不到它 - 您必须在响应中看到它。显然,异常是在 Json 方法之后抛出的。

关于c# - Ajax调用成功失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15928693/

相关文章:

c# - asp/mvc应用中使用错误提示

javascript - 将 src 值从 javascript 放置到 img 标签

javascript - 将 HTML 表单字段转换为具有内部对象的 JSON 对象

php - 通过 Ajax 从 Woocommerce 中的购物车商品数据中获取重命名的购物车商品名称

javascript - 在 PHP 中的 dhtmlx 调度程序中添加自定义字段

c# - 如何解决 HTTP Error 403.14 - Forbidden in Asp.Net Core API?

c# - 在 WPF 中,在项目中使用 Struct 或 Class 作为具有 MVVM 模式的模型?

c# - C# 中的快速随机生成器

Javascript onClick 解除绑定(bind)

php - 如何允许 php 文件仅由站点调用,而不是通过 Inspect Element 或 URL 调用