Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
967 views
in Technique[技术] by (71.8m points)

jquery - The length of the string exceeds the value set on the maxJsonLength property

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...