我正在对 aspx.cs 页面中的后端 WebMethod 进行 jQuery AJAX 调用。我在 .NET JSON 序列化中遇到错误。因此,我正在寻找修复错误或避免使用 JSON(WebMethods 的唯一返回格式)的方法:
Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property
相关的 StackTrace 是:
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。
感谢您的任何想法和建议!