Ajax错误 – jquery数据表

我在我的应用程序中使用jquery数据表。

当我尝试从服务器绑定json响应时,我在浏览器中收到以下消息

DataTables warning:table id=DataTables_Table_0-Ajax error.For more information about this error please seee http://datatables.net/tn/7

并在浏览器的控制台中

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

当我深入到控制台时,我得到以下exceptionA circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.CenterCode_16F8807C95C21FEFA99B4700E38D8ACB4A88C8E560B5640BD5E1FA148C99CCA5'. 我不知道这意味着什么?

Josn成功回归没有任何错误?我该如何解决这个问题?

JsonResultfunction

  public JsonResult GetDataTable(string finYear) { try { Common _cmn = new Common(); List _dTableReg = new List(); _dTableReg = _db.StudentRegistrations .AsEnumerable() .Where(r => _centerCodeIds.Contains(r.StudentWalkInn.CenterCode.Id) && (r.TransactionDate.Value.Date >= _startFinDate && r.TransactionDate.Value.Date  new RegistraionVM.RegDataTable { Centre = r.StudentWalkInn.CenterCode.CentreCode, CourseFee = r.TotalCourseFee.Value, Discount = r.Discount.Value, CurrEmpId = Int32.Parse(Session["LoggedUserId"].ToString()), WalkInn = r.StudentWalkInn, Receipt = r.StudentReceipts.Where(rc => rc.Status == false).FirstOrDefault(), RegDate = r.TransactionDate, RegistrationID = r.Id, SalesPerson = r.StudentWalkInn.CROCount == (int)EnumClass.CROCount.ONE ? r.StudentWalkInn.Employee1.Name : r.StudentWalkInn.Employee1.Name + "," + r.StudentWalkInn.Employee2.Name, StudentName = r.StudentWalkInn.CandidateName, SoftwareUsed = string.Join(",", r.StudentRegistrationCourses .SelectMany(c => c.MultiCourse.MultiCourseDetails .Select(mc => mc.Course.Name))), IsSalesIndividual = _currentRole == (int)EnumClass.Role.SALESINDIVIDUAL ? true : false }).OrderByDescending(r => r.RegistrationID).ToList(); return Json(new { data = _dTableReg }, JsonRequestBehavior.AllowGet); } catch (Exception ex) { return Json(new { data = "" }, JsonRequestBehavior.AllowGet); } } 

RegDataTable

  public class RegDataTable { public int RegistrationID { get; set; } public DateTime? RegDate { get; set; } public string Centre { get; set; } public string SalesPerson { get; set; } public string StudentName { get; set; } public int Discount { get; set; } public int CourseFee { get; set; } public string SoftwareUsed { get; set; } public StudentReceipt Receipt { get; set; } public bool IsSalesIndividual { get; set; } public StudentWalkInn WalkInn { get; set; } public int CurrEmpId { get; set; } public int? NextDueAmount { get { return Receipt == null ? 0 : Receipt.Total; } } public string NextDueDate { get { return Receipt == null ? "" : Receipt.DueDate.Value.ToString("dd/MM/yyyy"); } } public string MobileNo { get { if (IsSalesIndividual) { if ((CurrEmpId == WalkInn.CRO1ID) || (CurrEmpId == WalkInn.CRO2ID)) { return WalkInn.MobileNo; } else { return "-"; } } else { return WalkInn.MobileNo; } } } } 

HTML

 
RegDate Centre Sales Person Student Name Mobile S/W Used Discount CourseFee Next DueDetails NextDueAmount

从javascript调用DataTable

  table = $(".dTable").dataTable({ ... ..., columns: [ { "data": "RegDate" }, { "data": "Centre" }, { "data": "SalesPerson" }, { "data": "StudentName" }, { "data": "MobileNo" }, { "data": "SoftwareUsed" }, { "data": "Discount" }, { "data": "CourseFee" }, { "data": "NextDueDate" }, { "data": "NextDueAmount" }, { "data": "RegistrationID" } ], //Defining checkbox in columns "aoColumnDefs": [ { "targets": [0], "render": function (data, type, full, meta) { var date = new Date(parseInt(data.substr(6))); var month = date.getMonth() + 1; return date.getDate() + '/' + month + '/' + date.getFullYear() } }, { "targets": [8], "bSortable": false, "render": function (data, type, row) { if (row.NextDueAmount != 0) { return data + ',' + row.NextDueAmount } else { return "FULL PAID" } } }, ], }); 

JsonResponse 在此处输入图像描述

你应该返回Json(结果)。 构建列表后,如果抛出exception,则只返回空数据。 将您的方法结果更改为:

 return Json(result); } catch (Exception ex) { return Json(new { error = ex.Message }); } } 

您*必须在表格中添加空白

标签,例如;

 ...

这样dataTable脚本就可以向tbody添加数据并初始化表。

对我来说,我没有在渲染的“数据”中得到任何导致问题的东西。所以我使用“完整”而不是“数据”,它对我有用。

更正:

 "render": function (data, type, full, meta) { var date = new Date(parseInt(full.substr(6))); var month = date.getMonth() + 1; return date.getDate() + '/' + month + '/' + date.getFullYear() }