jQuery 字符串的长度超过了 maxJsonLength 属性上设置的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11881198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The length of the string exceeds the value set on the maxJsonLength property
提问by swpnilprakashpatil
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.
maxJsonLength
像这样更改Web.config 中属性的长度无济于事。
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644" />
</webServices>
</scripting>
</system.web.extensions>
</configuration>
Can anyone help me solve this?
谁能帮我解决这个问题?
回答by Taha Rehman Siddiqui
JavaScriptSerialzer has a public property named MaxJsonLength according to
JavaScriptSerialzer 有一个名为 MaxJsonLength 的公共属性,根据
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 解决了这个问题,并解决了一个困扰我很久的问题。
回答by lewiSnort
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
};
}
回答by Riain McAtamney
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 herefor an alternative
见这里的替代