javascript Ajax 字符串长度限制?

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

Ajax string length limit?

javascriptajax

提问by sawa

Is there a limit to the length of the parameter that be can added to the url for ajax? I am using Thin server on Ruby, and did an ajax request from the web browser in this format:

可以添加到 ajax url 的参数长度是否有限制?我在 Ruby 上使用瘦服务器,并以这种格式从 Web 浏览器发出 ajax 请求:

io=new XMLHttpRequest();
io.open("GET","http://localhost:3000&v="+encodeURIComponent(JSON.stringify(v)),true);

When the length of the string vexceeds about 7000 bytes, it seems to crash. When less, it seems to work. Is my observation right? Where is the restriction coming from? From Thin, Javascript, or the browser? I use Google Chrome browser.

当字符串长度v超过7000字节左右时,就好像崩溃了。当较少时,它似乎有效。我的观察对吗?限制从何而来?来自 Thin、Javascript 或浏览器?我使用 Google Chrome 浏览器。

回答by Darin Dimitrov

Is there a limit to the length of the parameter that can added to the url for ajax?

ajax可以添加到url的参数长度有限制吗?

Yes, if you are using a GET request there's a limit which will depend on the client browser. And this limit has nothing to do with AJAX. IIRC it was around 4K for IE but things might have changed. But in any case there's a limit. If you don't want to be limited you should use POST.

是的,如果您使用的是 GET 请求,则存在一个取决于客户端浏览器的限制。而这个限制与 AJAX 无关。IIRC 对于 IE 来说大约是 4K,但事情可能已经改变了。但无论如何都有一个限度。如果您不想受到限制,您应该使用 POST。

回答by socha23

Restriction most likely comes from the browser. According to this discussionyou should try to keep your URLs under about 2000 characters.

限制很可能来自浏览器。根据这个讨论,您应该尽量将您的 URL 保持在大约 2000 个字符以内。

回答by Panagiotis

There is a limit to the GET request depending on the character bytes. If you use ASCII it's 256 characters including the url itself. For UTF-8 it's practically the half because 1 utf character is 2bytes long.

GET 请求有限制,具体取决于字符字节。如果您使用 ASCII,则它是 256 个字符,包括 url 本身。对于 UTF-8,它实际上是一半,因为 1 个 utf 字符长 2 个字节。

You won't have this problem on POST though.

不过你在 POST 上不会有这个问题。