简体   繁体   English

Ajax调用成功失败

[英]Ajax call success is failing

This ajax call never creates the alert on success even though it has reached and returned success = true on the server side method. 这个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>

This is the server side method in my controller. 这是我的控制器中的服务器端方法。 I have tried to replace JsonResult with ActionResult, but it didn't change the outcome. 我试图用ActionResult替换JsonResult,但它没有改变结果。

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

You're getting an exception on the server - try debugging the .NET code, or watching your response with the browser tools, to see it. 您在服务器上遇到异常 - 尝试调试.NET代码,或者使用浏览器工具观察您的响应,以查看它。

If you want to return a JSON object on a GET method, you need to include a JsonRequestBehavior parameter to the Json call, like: 如果要在GET方法上返回JSON对象,则需要在Json调用中包含JsonRequestBehavior参数,如:

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

EDIT 编辑

Actually, it looks like you can't see it if you debug on the server - you'd have to see it in the response. 实际上,如果你在服务器上进行调试,看起来你看不到它 - 你必须在响应中看到它。 Apparently the exception is thrown further down after the Json method. 显然,在Json方法之后,异常会被进一步抛出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM