简体   繁体   中英

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.

@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.

 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.

If you want to return a JSON object on a GET method, you need to include a JsonRequestBehavior parameter to the Json call, like:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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