javascript JSON 参数大小限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9121158/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:40:03  来源:igfitidea点击:

JSON parameter size limit

javascriptjquerywcfjsonpost

提问by Dmitri Mogilevski

I am calling my WCF Web service using jQuery $.ajax json POST.

One of the input parameters is very long - over 8000 bytes. The data in it is a comma-separated list of GUIDs, like this "78dace54-1eea-4b31-8a43-dcd01e172d14,ce485e64-e7c6-481c-a424-2624371180aa,ede4c606-f743-4e0a-a8cc-59bcffa7feda,f0a81ed1-80db-4f6d-92d7-2fc47759a409".

When that parameter is 8176 byteslong, the request succeeds. When it's 8213(one more comma and GUID) - the request fails.

It fails from the browser and from Fiddler (HTTP debugging proxy). I added this to the webservice config:

我正在使用 jQuery $.ajax json POST 调用我的 WCF Web 服务。

其中一个输入参数很长 - 超过 8000 字节。其中的数据是一个以逗号分隔的 GUID 列表,例如“78dace54-1eea-4b31-8a43-dcd01e172d14,ce485e64-e7c6-481c-a424-2624371180aa,ede4c69-dab70a-cc6306-aff-acc8f-af7a-fe-e 4f6d-92d7-2fc47759a409"。

当该参数为8176 字节长时,请求成功。当它是8213(一个逗号和 GUID)时 -请求失败

它从浏览器和 Fiddler(HTTP 调试代理)失败。我将此添加到网络服务配置中:

<configuration>
<system.web.extensions>
    <scripting>
        <webServices>
            <jsonSerialization maxJsonLength="50000000" recursionLimit="50000"/>
        </webServices>
    </scripting>
</system.web.extensions>

That does not make any difference, the request still fails for input param over 8176 bytes long.
That input param maps into a String on the WCF side.
What am I missing? Thank you!

这没有任何区别,对于超过 8176 字节长的输入参数,请求仍然失败。
该输入参数映射到 WCF 端的字符串。
我错过了什么?谢谢!

UPDATE, this solved my problem: Turns out that this setting controls the total JSON message length

更新,这解决了我的问题:原来这个设置控制总 JSON 消息长度

<webServices>
     <jsonSerialization maxJsonLength="50000000" recursionLimit="50000"/>
</webServices>

There is another setting that controls maximum length for individual parameters:

还有另一个设置可以控制单个参数的最大长度:

<bindings>
  <webHttpBinding>
    <binding name="Binding_Name" maxReceivedMessageSize="900000">
      <readerQuotas maxDepth="32" maxStringContentLength="900000" maxBytesPerRead="900000" maxArrayLength="120000" maxNameTableCharCount="120000"/>
    </binding>
  </webHttpBinding>
</bindings>

Also, make sure to set this:

另外,请确保设置:

  <system.web>
     <httpRuntime maxRequestLength="900000"/>

Hope this takes care of some headaches out there!

希望这能解决一些令人头疼的问题!

回答by LD.

The actual limit seems to be 8192 bytes.

实际限制似乎是 8192 字节。

You have to check your Web.config in the system.serviceModel tag :

您必须在 system.serviceModel 标签中检查您的 Web.config :

 <system.serviceModel>
  <bindings>
   <basicHttpBinding>
     <binding name="Service1Soap" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
     <security mode="None">
      <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
      <message clientCredentialType="UserName" algorithmSuite="Default"/>
     </security>
    </binding>
   </basicHttpBinding>
  </bindings>

You need to change maxStringContentLength="8192"to a greater value.

您需要将maxStringContentLength="8192"更改为更大的值。

You may also make multiple requests instead of one to get the list of GUID page by page, using an offset parameter in each request. For example, to get list of GUID by pages of 200, first request with offset=0, second with offset=200, ... until you get less than 200 items.

您还可以发出多个请求而不是一个请求来逐页获取 GUID 列表,在每个请求中使用偏移参数。例如,要按 200 页获取 GUID 列表,第一个请求偏移量为 0,第二个请求偏移量为 200,...直到获得少于 200 个项目。

回答by álvaro González

I know it won't be of much help for you but I'd like to point out that the JSON spec does not set any limit; however, it allows parsers to do so:

我知道这对你没有多大帮助,但我想指出 JSON 规范没有设置任何限制;但是,它允许解析器这样做:

An implementation may set limits on the size of texts that it
accepts. An implementation may set limits on the maximum depth of
nesting. An implementation may set limits on the range of numbers.
An implementation may set limits on the length and character contents of strings.

实现可能会对其
接受的文本大小设置限制。一个实现可以对
嵌套的最大深度设置限制。一个实现可以对数字范围设置限制。
一个实现可以对字符串的长度和字符内容设置限制。

RFC4627: The application/json Media Type for JavaScript Object Notation (JSON)

RFC4627:JavaScript 对象表示法 (JSON) 的 application/json 媒体类型

See if this answerapplies to you.

看看这个答案是否适用于你。