Ajax请求成功但是一直进入error 之 序列化类型为“System.Reflection.RuntimeModule”的对象时检测到循环引用。解决方案

前端代码:

$.ajax({
     url: $("#dataMain").attr("data-url"),
     type: "GET",
     dataType: "json",
     timeout: 3000,
     data: data,
     success: function (res) {
          console.log(res);
      },
      error: function (res) {
           console.log(res);
      }
});

后端代码:

 [HttpGet]
 public JsonResult Test1() 
 {
     var sql = string.Format(@"SELECT * FROM RELEASE ");
DataTable dataTable
= GlobalContext.Resolve<ISource_Web_SQLHelper>().GetDataSet(sql, "你的数据库链接串").Tables[0]
return Json(dataTable, JsonRequestBehavior.AllowGet); }

结果:总是跑到 Ajax的 error 方法中

error: function (res) {
    console.log(res);
}

解决方案一:后端代码将 dataTable 先经过下列操作再返回。

JsonSerializerSettings setting = new JsonSerializerSettings()
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};

var data = JsonConvert.SerializeObject(dataTable, setting);
return Json(data, JsonRequestBehavior.AllowGet);

解决方案二:后端代码将 dataTable 转化成 List 再返回 。

var data=dataTable.AsEnumerable().ToList();
return Json(data, JsonRequestBehavior.AllowGet);

Java: Ajax请求成功但是一直进入error的原因 

猜你喜欢

转载自www.cnblogs.com/mww-NOTCOPY/p/12341768.html
今日推荐