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的原因 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM