2017-07-27 84 views
-3

我將數據從mvc返回給ajax。但數組並沒有渲染成ajax成功函數給出內部服務器錯誤。我的代碼附在下面。從MVC4控制器返回json數據到ajax(不工作)

我的Ajax調用:

$.ajax({ 

      type: "POST", 
      url: "/Home/getallNews/", 

      success: function (data) { 
       debugger; 
       console.log(data); 
       alert(data); 

      } 
       , function (error) { 
        alert(JSON.stringify(error)); 
       } 
}); 

和MVC控制器動作:

[HttpPost] 
    public JsonResult getallNews() 
    { 
     //var id = 16; 

     var Returnmodel = _newsRepository.GetAll().ToList(); 

     return Json(Returnmodel, JsonRequestBehavior.AllowGet); 


    } 

enter image description here

enter image description here

+0

使用瀏覽器工具(網絡選項卡)來檢查響應。什麼是錯誤的細節? –

+0

'/'應用程序中的服務器錯誤。而序列化類型的對象「System.Data.Entity.DynamicProxies.EntityNews_A56D36119E4D7880562B6207DD29EC42DDA0BAED4A20567E748CC8A346B9E1EE」檢測 循環引用。 –

+0

請求URL:http:// localhost:31781/Home/getallNews/ 請求方法:POST 狀態碼:500內部服務器錯誤 遠程地址:[:: 1]:31781 引薦策略:no-referrer-when-降級 –

回答

1

改變你的Ajax調用:

$.ajax({ 
       url: "/Home/getallNews/", 
       type: "POST", 

       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       error: function (response) { 
        alert(response.responseText); 
      }, 
       success: function (response) { 
        alert(response); 
       } 
      }); 

     }); 
+0

這個AJAX調用它顯示錯誤消息 –

+0

這些都不是必要的,這有什麼都沒有做OP的問題 –

+0

@ umersafeer該錯誤消息既不的 –

1

添加的contentType和數據類型在你的Ajax調用如下

$.ajax({ 
      url: "@Url.Action("getallNews","Home")", 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (response) { 
       alert(response); 
      },         
      error: function (response) { 
       alert(response.responseText); 
      } 
      }); 
+0

這些都是必要的,這有什麼好在所有與OP問題做 –

+0

給予同樣的錯誤消息與此調用以及 –

+0

你在上面添加任何授權屬性? –