Json result is not displaying

廉价感情. 提交于 2019-12-25 19:49:07

问题


    I am facing issue while displaying the result from a ajax call.
    Contrller json method which returns data from database.

The control is coming in the success block of my ajax call but the data is empty. if i debug the controller GetNotifications method it is returning the data. Can somebody help me with this

     public JsonResult  GetNotifications()
            {

                IList<Notification> notificationsBody = db.Notifications.ToList();

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

            }`View used to display and call ajax is 

@model IEnumerable<NotificationApp.Model.Notification> 
<h2>
    Notification</h2>
@using (Html.BeginForm())
{
    <ul id="nav">
        <li id="notification_li"><span id="notification_count"></span><a href="#" id="notificationLink">
            Notifications</a>
            <div id="notificationContainer">
                <div id="notificationTitle">
                    Notifications</div>
                <div id="notificationsBody" class="notifications">
                </div>
                <div id="notificationFooter">
                    <a href="#">See All</a></div>
            </div>
        </li>
    </ul>
}
@section Scripts {
    <script src="~/Scripts/jquery-2.1.4.min.js" type="text/javascript"></script>
    <script src="~/Scripts/jquery.signalR-1.1.4.js" type="text/javascript"></script>
    <script src="~/signalr/hubs"></script>
    <script type="text/javascript">
        $(function () {
            var proxy = $.connection.notificationHub;
            proxy.client.receiveNotification = function (message, UnreadCount) {
                $("#notification_count").html(UnreadCount);
                $("#notification_count").show();
                $("#container").slideDown(2000);
                setTimeout('$("#container").slideUp(2000);', 5000);
            };
            $.connection.hub.start();

        });
    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#notificationLink").click(function () {
                $("#notificationContainer").fadeToggle(300);
                $("#notification_count").fadeOut("slow");

                var tbl = $('#notificationsBody');

                $.ajax({
                    url: '/Notification/GetNotifications',
                    contentType: 'application/json ; charset:utf-8',
                    type: 'POST',
                    dataType: 'json'
                }).success(function (result) {
                    alert(1);
                    alert(result[0].NotificationID);
                    tbl.empty().append(result);
                }).error(function () {
                    alert(13);
                });

                //success: function (data) {
                //if (data) {
                //var len = data.length;
                //var txt = "";
                //if (len > 0) {
                //for (var i = 0; i < len; i++) {
                //if (data[i].Name && data[i].Address) {
                //txt += "<tr><td>" + data[i].Name + "</td><td>" + data[i].Address + "</td></tr>"
                //  + "<td>" + data[i].PhoneNo + "</td>" + "<td>" + data[i].Country + "</td></tr>";
                //}
                //}
                //if (txt != "") {
                //$("#EmployeesTable").append(txt);
                //alert(txt);
                //}
                //}
                //}
                //},



            });
            return false;

            //Document Click hiding the popup 
            $(document).click(function () {
                $("#notificationContainer").hide();
            });

            //Popup on click
            $("#notificationContainer").click(function () {
                return false;
            });

        });
    </script>
}
`

here i am getting the Alert which is inside the success block. But the line alert(result[0].NotificationID); is not displaying any alert.


回答1:


Since I don't have enough reputation I can't write this as an comment:

I think, your question is unanswered because it is a bit unclear, what you are asking for..




回答2:


first don't use alert that dialog box will be annoying use console.log(result) or console.log(JSON.stringify(result)) and press f12 in chrome to see the console. If nothing return than might be problem in controller. Verify that

 return Json(notificationsBody , JsonRequestBehavior.AllowGet);

you can just send the object no need to do notificationsBody = notificationsBody

when you get the list use $.each method to iterate over your data.

i don't know why peoples use this in ajax when not sending data over to server

contentType: 'application/json ; charset:utf-8',

just use parameters that you need.



来源:https://stackoverflow.com/questions/34594558/json-result-is-not-displaying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!