我正在对aspx.cs页面中的后端WebMethod进行jQuery AJAX调用。我在.NET JSON序列化中遇到错误。因此,我想办法要么修复错误或避免使用JSON(为的WebMethods只返回格式):WebMethod - JSON序列化错误
Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property
相关的堆栈跟踪是: at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)\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)
后端代码如下(注意:result
是,在现实中呈现左右为一个字符串控制的110K):
[WebMethod]
public static string GetContactListControl()
{
try
{
var result = "Hello World!"
return result;
}
catch (Exception e){
Logging.LogException(e);
return "Exception Thrown";
}
}
而且我从来没有击中catch
块,这对我来说显示这个问题在我的代码之外。
我发现涉及插入以下块改变web.config中的修复程序,但它不工作:
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="123456"></jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
项目是.NET 3.5。
谢谢你的任何想法和建议!
我假设你意味着,在方法的开头设置一个断点,当你说这是从来没有击中它“永远不会碰到”catch“块”。用代码,我真诚地怀疑'返回'Hello World!''可能会抛出异常。 – 2012-07-05 20:06:55
尝试增加'maxJsonLength'多一吨。大约110kb大约是112,640字节,与123,456字节不太相近。也许额外的开销(或“大约”不是太准确)正在突破限制。尝试冲压到9999999,看看会发生什么。 – 2012-07-05 20:09:03
正确的断点很好。当我在_return_ _result_上打破时,它显示结果的值没有任何问题。 – WhistlingZebra 2012-07-05 20:10:54