最近發現了個問題: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))]);