String exceeds maxJsonLength with less than 250KB

I use an entity with ajax. I want the full table to be created using the Entity infrastructure in a grid created using JavaScript. The table I'm sending now contains less than 140 rows. My code works if I have only 50 rows in the table and 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 statistics, I get a 500 error for my XMLHttpRequest in the response body. I have an error as shown above. If I limit the number of lines to 50, I can make the page work correctly, and I can see my data in the response body. The following code is the code needed to send data from your object to your javascript. JavaScript:

$(function() {
    $.ajax({
        url: "EditFeedApac.aspx/GetApacData",
        type: "POST",
        contentType: "application/json",
        success: function (result) {
            console.log(result.d);
        },
        error: showError
    });
});

WITH#

[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 4 , . ? ? JavaScript?

+3
1

<configuration> 
  <system.web.extensions>
    <scripting>
       <webServices>
           <jsonSerialization maxJsonLength="50000000"/>
       </webServices>
    </scripting>
  </system.web.extensions>
</configuration> 

,

serializer.MaxJsonLength = Int32.MaxValue;

.

+7

All Articles