The length of the string exceeds the value set on the maxJsonLength property
Asked Answered
A

3

27

I am loading tab content data through jQuery's ajax post method via web method with around 200-300 records. And getting following error in the console:

Error: Sys.Net.WebServiceFailedException: Sys.Net.WebServiceFailedException: System.InvalidOperationException-- Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

Changing the length of the maxJsonLength attribute in Web.config like this does not help.

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

Can anyone help me solve this?

Averment answered 9/8, 2012 at 10:17 Comment(3)
Perhaps your data still exceeds this limit?Clout
I have the same problem, my data does not exceed the limit so the answer below doesn't help me.Coheman
If anyone else has this happen to them, it was answered here: #1152487Hughhughes
S
37

JavaScriptSerialzer has a public property named MaxJsonLength according to

http://msdn.microsoft.com/en-us/library/system.web.configuration.scriptingjsonserializationsection.maxjsonlength.aspx

Now, where you are deserializing your json, use this

JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue; //Or any size you want to use, basically int maxValue is 2GB, you shouldn't need this big json string to deserialize, else you are doing it wrong.
myObject obj = serializer.Deserialize<myObject>(yourJsonString);

And this should work perfectly, I recently figured this out through msdn and solved a problem that was bugging me for long.

Skye answered 26/3, 2013 at 20:52 Comment(0)
A
7

I know this is a very old thread at the time of my reading, and that WebMethod is not really a thing in ASP.NET MVC so this is somewhat tangential. But, if anyone runs across it -

If you use ASP.NET MVC There is an alternative to invoking JavaScriptSerializer directly, which is to initialize the JsonResult and set the MaxJsonLength property on the result itself:

    private JsonResult GetReallyBigJsonResult(object data, JsonRequestBehavior requestBehavior)
    {
            return new JsonResult()
            {
                ContentEncoding = Encoding.Default,
                ContentType = "application/json",
                Data = data,
                JsonRequestBehavior = requestBehavior,
                MaxJsonLength = int.MaxValue
            };

    }
Alarmist answered 23/6, 2016 at 18:39 Comment(0)
C
0

Not knowing the size of your string but perhaps it still exceeds the max limit that you have set?

Ideally ajax scenarios are meant only for small to medium server side calls not for getting tons of data, and if you are getting a lot of data using the asynchronous request then you are asking for trouble.

See here for an alternative

Clout answered 9/8, 2012 at 10:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.