字符串的长度超过maxJsonLength属性上设置的值

时间:2022-05-05 20:23:45

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:

我正在通过jQuery的ajax post方法通过web方法加载标签内容数据,大约有200-300条记录。并在控制台中收到以下错误:

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.

错误:Sys.Net.WebServiceFailedException:Sys.Net.WebServiceFailedException:System.InvalidOperationException--使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过maxJsonLength属性上设置的值。

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

像这样在Web.config中更改maxJsonLength属性的长度没有帮助。

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

Can anyone help me solve this?

谁能帮我解决这个问题?

3 个解决方案

#1


31  

JavaScriptSerialzer has a public property named MaxJsonLength according to

JavaScriptSerialzer有一个名为MaxJsonLength的公共属性

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

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

Now, where you are deserializing your json, use this

现在,在您对json进行反序列化的地方,请使用此方法

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.

这应该是完美的,我最近通过msdn解决了这个问题并解决了一个长期困扰我的问题。

#2


5  

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 -

我知道这是我阅读时的一个非常老的线程,并且WebMethod在ASP.NET MVC中并不是真正的东西,所以这有些切合实际。但是,如果有人遇到它 -

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:

如果使用ASP.NET MVC有一种替代方法可以直接调用JavaScriptSerializer,即初始化JsonResult并在结果本身上设置MaxJsonLength属性:

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

    }

#3


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.

理想情况下,ajax方案仅适用于中小型服务器端调用,而不是用于获取大量数据,如果您使用异步请求获取大量数据,那么您就会遇到麻烦。

See here for an alternative

请看这里的替代方案

#1


31  

JavaScriptSerialzer has a public property named MaxJsonLength according to

JavaScriptSerialzer有一个名为MaxJsonLength的公共属性

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

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

Now, where you are deserializing your json, use this

现在,在您对json进行反序列化的地方,请使用此方法

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.

这应该是完美的,我最近通过msdn解决了这个问题并解决了一个长期困扰我的问题。

#2


5  

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 -

我知道这是我阅读时的一个非常老的线程,并且WebMethod在ASP.NET MVC中并不是真正的东西,所以这有些切合实际。但是,如果有人遇到它 -

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:

如果使用ASP.NET MVC有一种替代方法可以直接调用JavaScriptSerializer,即初始化JsonResult并在结果本身上设置MaxJsonLength属性:

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

    }

#3


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.

理想情况下,ajax方案仅适用于中小型服务器端调用,而不是用于获取大量数据,如果您使用异步请求获取大量数据,那么您就会遇到麻烦。

See here for an alternative

请看这里的替代方案