Jquery Ajax - 发布巨大的字符串值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17810063/
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
Jquery Ajax - post huge string value
提问by bombastic
i need to POST a huge string large about 3mb, is it possible to send it to php using not url params?
我需要发布一个大约 3mb 的巨大字符串,是否可以使用非 url 参数将其发送到 php?
If i send it in url params the request reachs the size limit in url.
如果我在 url params 中发送它,请求就会达到 url 中的大小限制。
How can work around this? any clue?
如何解决这个问题?任何线索?
thanks a lot.
多谢。
Actually i'm doing it like this:
其实我是这样做的:
$.ajax({
type:'POST',
.......
data:{string:3MB_string}
});
i'm using PHP and jQuery , and i wouldl ike to send the 3mb base64 string to php on a simple url, like site.com/script.php
我正在使用 PHP 和 jQuery,我想通过一个简单的 url 将 3mb base64 字符串发送到 php,例如 site.com/script.php
The string is a File Reader API base64 image
该字符串是文件读取器 API base64 图像
this is an example of string but this won't reach the size limit it is not a 3mb is less cause troubling in pasting that to show you a 3mb, http://jsfiddle.net/QSyMc/
这是一个字符串的例子,但这不会达到大小限制,它不是 3mb 是较少的原因,粘贴它以显示 3mb,http://jsfiddle.net/QSyMc/
回答by Darin Dimitrov
You will need to use a POST request:
您将需要使用 POST 请求:
$.ajax({
url: '/script.php',
type: 'POST',
data: { value: 'some huge string here' },
success: function(result) {
alert('the request was successfully sent to the server');
}
});
and in your server side script retrieve the value:
并在您的服务器端脚本中检索值:
$_POST["value"]
Also you might need to increase the allowed request size. For example in your .htaccess
file or in your php.ini
you could set the post_max_size
value:
此外,您可能需要增加允许的请求大小。例如在您的.htaccess
文件中或在您的文件中,您php.ini
可以设置post_max_size
值:
#set max post size
php_value post_max_size 20M
回答by krampstudio
Try with processData
to false and a string representation of your JSON
尝试使用processData
false 和 JSON 的字符串表示
var data = { "some" : "data" };
$.ajax({
type: "POST",
url: "/script",
processData: false,
contentType: 'application/json',
data: JSON.stringify(data),
success: function(r) {
console.log(r);
}
});
From the jQuery.ajaxdocumentation :
来自jQuery.ajax文档:
processData(default: true)
Type: Boolean
By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.
processData(默认值:true)
类型:布尔型
默认情况下,作为对象传递给 data 选项的数据(从技术上讲,除了字符串之外的任何内容)将被处理并转换为查询字符串,适合默认的内容类型“application/x-www-form-urlencoded” . 如果要发送 DOMDocument 或其他未处理的数据,请将此选项设置为 false。
回答by jdp
I came across a similar problem. It wasn't the length of the query string but rather the number of variables I was passing to the server. The php.ini places a limit in the max_input_vars field to 1200 variables. In my case, I was exceeding that amount rather than the post_max_size amount. Had to go back and make the query more efficient and under the limit. I guess I could have raised the php.ini setting but I wound up with better code by disabling non-essential query parameters.
我遇到了类似的问题。这不是查询字符串的长度,而是我传递给服务器的变量数。php.ini 将 max_input_vars 字段限制为 1200 个变量。就我而言,我超出了该数量而不是 post_max_size 数量。不得不回去使查询更有效并在限制之下。我想我可以提高 php.ini 设置,但通过禁用非必要的查询参数,我最终得到了更好的代码。
回答by Artem Kiselev
You definitely need to use POST method. If the string is still to large, check you php.ini file to find out the maximum POST parameter size.
您肯定需要使用 POST 方法。如果字符串仍然很大,请检查 php.ini 文件以找出最大的 POST 参数大小。
To change this value value, do any one of those:
要更改此值,请执行以下任一操作:
1. change values in php.ini
1.修改php.ini中的值
post_max_size=20M
upload_max_filesize=20M
2. or add this code to .htaccess file
2.或将此代码添加到.htaccess文件中
php_value post_max_size 20M
php_value upload_max_filesize 20M
Which one to use depends on what you have access to.
使用哪一种取决于您可以访问的内容。
回答by Dhaval Bharadva
Try this:
尝试这个:
var str = "YOUR STRING";
$.ajax({
url: 'YOUR URL',
type: 'POST',
data: { mystring: str },
success: function(result) {
console.log(result);
}
});