最近发现了个问题:ajax post请求查询参数数量动态变化有200-250000个,当参数超过一定数量N时,post传到后台接的参数就只有N个,多出的参数都没附到请求中,这也是奇怪的事情,浏览器对参数的个数有限制。
js
const payload = { date: "2024-05-10", sn: [] };
for (let i = 1; i < 10000; i++) {
payload["sn"].push("aaaaaaaaaaaaaaaaaaaaaa");
}
$.ajax({
type: "post",
url: "../test/request.php",
data: payload,
beforeSend: function () {},
success: function (res) {
console.log(res);
},
error: function (xhr1) {},
});
php
<?php
ini_set('max_input_vars', 200000); //未生效
$data = $_REQUEST;
echo json_encode(['date' => $data['date'], 'num' => count($data['sn'])]);
结果如下,使用ini_set临时修改max_input_vars,并未生效,大概只能去php.ini中修改了
最终解决方案:将数组参数变成字符串参数,减少传递的参数个数
js
const payload = { date: "2024-05-10", sn: "" };
const sn = [];
for (let i = 1; i <= 20000; i++) {
sn.push("aaaaaaaaaaaaaaaaaaaaaa");
}
payload["sn"] = JSON.stringify(sn);
$.ajax({
type: "post",
url: "../test/request.php",
data: payload,
beforeSend: function () {},
success: function (res) {
console.log(res);
},
error: function (xhr1) {},
});
php
<?php
$data = $_REQUEST;
echo json_encode(['date' => $data['date'], 'num' => count(json_decode($data['sn'], true))]);
标签:截断,ajax,参数,sn,date,data,payload From: https://www.cnblogs.com/caroline2016/p/18193578