2017-07-27 88 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

你在上面添加任何授权属性? –