【问题标题】:The length of the string exceeds the value set on the maxJsonLength property (WebMethod)字符串长度超过 maxJsonLength 属性(WebMethod)上设置的值
【发布时间】:2015-05-30 02:52:06
【问题描述】:

背景

我在 ASPX 页面的代码隐藏中有一个 WebMethod

[WebMethod]
public static string GetServiceDetail()
{
    //...
}

我正在使用 jQuery 向它发送 AJAX 请求:

$.ajax({
    type: "POST",
    url: url + "GetServiceDetail",
    data: JSON.stringify({}),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
})

这通常可以正常工作。

问题

WebMethod 返回大约 88KB 的数据时,框架会向 HTTP 请求返回一个500 Internal Server Error。 HTTP 响应正文包含一堆 JSON 格式的 .NET 异常信息。反序列化后,响应包含以下信息:

System.InvalidOperationException:

使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了在maxJsonLength 属性上设置的值。

   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)
   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)
   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)
   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)

疑难解答

  • 我根本没有在WebMethod 中使用JavaScriptSerializer。正如堆栈跟踪所示,异常发生在我的代码之外。
  • 现有的 StackOverflow 问题似乎与显式 JSON 序列化或不同技术有关。它们意味着更改 Web.config 值不起作用。

【问题讨论】:

    标签: asp.net json webforms webmethod


    【解决方案1】:

    我通过更改Web.config 中名为maxJsonlength 的属性解决了这个问题:

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

    来自https://stackoverflow.com/a/1151993/238753

    【讨论】:

      猜你喜欢
      • 2012-08-06
      • 2015-03-27
      • 2018-04-25
      • 2017-08-31
      • 2013-05-26
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      相关资源
      最近更新 更多