JSON结果不显示 [英] Json result is not displaying

查看:247
本文介绍了JSON结果不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

控制在我的AJAX调用的成功来块,但数据是空的。如果我调试控制器GetNotifications方法,它返回的数据。有人可以帮我这个

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>
}
`

在这里我得到警报是成功的块中。但线
警报(结果[0] .NotificationID);
没有显示任何警报。

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

推荐答案

先不使用警告该对话框会讨厌使用的console.log(结果)或执行console.log(JSON.stringify(结果) )在Chrome和preSS F12看到控制台。如果没有收益比可能是控制器的问题。
验证

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

你可以发送的对象没有必要做的 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.

这篇关于JSON结果不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆