根据 John 的回答,我将 GET 请求更改为 POST 请求。它可以工作,无需更改服务器配置。所以我去寻找如何实现这一点。以下页面很有帮助:
jQuery Ajax POST example with PHP
(注意清理发布的数据备注)和
http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
基本上不同的是,GET请求将url和参数放在一个字符串中,然后发送null:
http.open("GET", url+"?"+params, true);
http.send(null);
而 POST 请求在单独的命令中发送 url 和参数:
http.open("POST", url, true);
http.send(params);
这是一个工作示例:
ajaxPOST.html:
<html>
<head>
<script type="text/javascript">
function ajaxPOSTTest() {
try {
// Opera 8.0+, Firefox, Safari
ajaxPOSTTestRequest = new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try {
ajaxPOSTTestRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxPOSTTestRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
ajaxPOSTTestRequest.onreadystatechange = ajaxCalled_POSTTest;
var url = "ajaxPOST.php";
var params = "lorem=ipsum&name=binny";
ajaxPOSTTestRequest.open("POST", url, true);
ajaxPOSTTestRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxPOSTTestRequest.send(params);
}
//Create a function that will receive data sent from the server
function ajaxCalled_POSTTest() {
if (ajaxPOSTTestRequest.readyState == 4) {
document.getElementById("output").innerHTML = ajaxPOSTTestRequest.responseText;
}
}
</script>
</head>
<body>
<button onclick="ajaxPOSTTest()">ajax POST Test</button>
<div id="output"></div>
</body>
</html>
ajaxPOST.php:
<?php
$lorem=$_POST['lorem'];
print $lorem.'<br>';
?>
我刚刚发送了超过 12,000 个字符,没有任何问题。