解决HTTP 414“请求URI太长”的思路

参考社区问答https://cloud.tencent.com/developer/ask/96327

1.不建议改apache的配置

在Apache下,限制是一个可配置的值。如果想要支持更长的请求URI,请将此值更改为大于默认值8190的值。该值位于/etc/apache2/apache2.conf中。如果不是,请LimitRequestLine 10000在下添加一个新行()AccessFileName .htaccess。

但是如果实际遇到此限制,可能会先滥用GET。你应该使用POST传输这种数据 - 尤其是因为你甚至承认你正在使用它来更新值。如果你检查上面的链接,你会注意到Apache甚至说:“在正常情况下,值不应该从默认值改变。”

2.主要在后端处理,前端改请求方法

基本上,区别在于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>';

?>

猜你喜欢

转载自blog.csdn.net/derkampf/article/details/88179944
今日推荐