apache 如何解决HTTP 414“请求URI太长”错误?

myzjeezk  于 2023-03-09  发布在  Apache
关注(0)|答案(6)|浏览(348)

我已经开发了一个PHP的网络应用程序。我给用户一个选项来更新多个问题一次去。在这样做的时候,有时候用户会遇到这个错误。有没有办法增加在Apache的URL的长度?

cgh8pdjw

cgh8pdjw1#

在Apache下,此限制是一个可配置的值**LimitRequestLine**。如果要支持更长的请求URI,请将此值更改为大于其默认值8190的值。该值位于 /etc/apache 2/apache2.conf 中。否则,请在AccessFileName .htaccess下添加新行(LimitRequestLine 10000)。
但是,请注意,如果您实际上遇到了这个限制,那么您可能一开始就在滥用GET。您应该使用POST来传输这类数据--特别是在您承认使用它来更新值之后。如果您查看上面的链接,您会注意到Apache甚至说“在正常情况下,不应该更改默认值”。

pn9klfpd

pn9klfpd2#

根据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多个字符,没有任何问题。

r55awzrz

r55awzrz3#

我有一个简单的解决方法。
假设您的URI中有一个字符串stringdata,它太长了,您可以根据服务器的限制将其分解为多个部分,然后提交第一个部分(在我的例子中是写一个文件),然后提交下一个部分以附加到先前添加的数据中。

x7rlezfr

x7rlezfr4#

我在使用$.getJSON()后得到这个错误。我只是改为发布:

data = getDataObjectByForm(form);
var jqxhr = $.post(url, data, function(){}, 'json')
    .done(function (response) {
        if (response instanceof Object)
            var json = response;
        else
            var json = $.parseJSON(response);
        // console.log(response);
        // console.log(json);
        jsonToDom(json);
        if (json.reload != undefined && json.reload)
            location.reload();
        $("body").delay(1000).css("cursor", "default");
    })
    .fail(function (jqxhr, textStatus, error) {
        var err = textStatus + ", " + error;
        console.log("Request Failed: " + err);
        alert("Fehler!");
    });
rlcwz9us

rlcwz9us5#

摘自**RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1:**

POST方法用于请求源服务器接受请求中包含的实体,作为请求行中请求URI所标识资源的新从属实体。POST设计为允许统一方法涵盖以下功能:

  • 现有资源的注解;
  • 将消息张贴到公告板、新闻组、邮件列表或类似的文章组;
    *向数据处理过程提供数据块,例如提交表格的结果;
  • 通过追加操作扩展数据库。
7fyelxc5

7fyelxc56#

我在使用Postman测试woocommerce rest API时也遇到了这个错误。
问题是因为授权数据是在请求正文中发送的。当我在标题中发送它们时,问题就解决了。

相关问题