我正在和ajax一起使用一个实体.我希望在使用
JavaScript创建的网格中使用实体框架提供完整的表格.我目前发送的表格少于140行.我的代码工作,如果我在表中只有50行,我得到以下错误:
{"Message":"Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.","StackTrace":" at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj,StringBuilder output,SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj,SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context,WebServiceMethodData methodData,IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,WebServiceMethodData methodData)","ExceptionType":"system.invalidOperationException"}
在我的网络统计中,我的响应正文中的XMLHttpRequest出现500错误,我有错误,如上所示.如果我将行数限制为50,我可以让页面正常工作,我可以在响应正文中看到我的数据.以下代码是将实体数据发送到javascript所需的所有代码.
JavaScript的:
$(function() { $.ajax({ url: "EditFeedApac.aspx/GetApacData",type: "POST",contentType: "application/json",success: function (result) { console.log(result.d); },error: showError }); });
C#
[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static IEnumerable<PreApacData> GetApacData() { var a = new Towers_WatsonEntities().tbl_TowersWatson_preAPAC.ToList(); Mapper.CreateMap<tbl_TowersWatson_preAPAC,PreApacData>(); var newData = a.Select(Mapper.Map<tbl_TowersWatson_preAPAC,PreApacData>).ToList(); return newData; }
如您所见,错误清楚地表明MaxJsonLength尚未设置为最大值.
在GetApacData()函数中,我添加了以下代码:
var serializer = new JavaScriptSerializer(); serializer.MaxJsonLength = Int32.MaxValue; string test = serializer.Serialize(newData); return test;
现在MaxJsonLength可以变成4MB的大小,只是为了取消限制.
使用此属性集我仍然得到相同的序列化错误?
我在这做错了什么?
如何将数据发送到我的JavaScript?