2016-01-04 89 views
-3
I am facing issue while displaying the result from a ajax call. 
    Contrller json method which returns data from database. 

控制進入我的ajax調用的成功塊,但數據是空的。如果我調試控制器的GetNotifications方法,它將返回數據。有人可以幫我這個Json結果不顯示

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

這裏我得到的是在成功塊內的警報。但行 alert(result [0] .NotificationID); 未顯示任何警報。

+0

當我試圖發佈一個queston時,它給出了一個警告,我的問題沒有回答。可以有人告訴我如何整理出 – Swathi

+0

我的問題沒有答案。我在問這個問題的時候錯過了一些東西嗎?可以有人引導我爲什麼這裏沒有回答問題嗎? – Swathi

回答

0

因爲我沒有足夠的聲譽,我不能寫這個作爲一個評論:

我覺得,你的問題是沒有答案,因爲它是有點不清楚,你問什麼..

0

首先不要使用警告,該對話框會令人討厭使用console.log(result)或console.log(JSON.stringify(result))並在chrome中按f12以查看控制檯。如果沒有任何回報可能是控制器中的問題。 驗證

return Json(notificationsBody , JsonRequestBehavior.AllowGet); 

你可以只發送對象沒有必要做notificationsBody = notificationsBody當你列表使用$。每方法將數據迭代

我不知道爲什麼人們用這個在阿賈克斯時,在不發送數據到服務器

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

僅使用你需要的參數。