I'm using an entity together with ajax. I want the full table i provice with Entity framework ina grid created with JavaScript. The table I'm currently sending has less than 140 lines. My code works if I only have 50 lines in the table a few more an I get the following error:
{"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"}
In my network stats I get a 500 error for my XMLHttpRequest in the response body I have the error as shown above. If I limit the amount of lines to 50 I can have the page working properly and I can see my data in the response body. The following code is all the code you need to send data from your entity to your 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;
}
As you can see the error clearly indicates MaxJsonLength hasn't been set to the max value. In the GetApacData() function I added the following code:
var serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
string test = serializer.Serialize(newData);
return test;
Now the MaxJsonLength could become the size of 4MB, just to take the limit off. With this property set I still get the same serialization error? What am I doing wrong here? How can I send my data to my JavaScript?