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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2891574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:01:39  来源:igfitidea点击:

How do I resolve a HTTP 414 "Request URI too long" error?

phpapachehttp-status-codes

提问by JPro

I have developed a PHP web app. I am giving an option to the user to update multiple issues on one go. In doing so, sometimes the user is encountering this error. Is there any way to increase the lenght of URL in apache?

我开发了一个 PHP 网络应用程序。我为用户提供了一次更新多个问题的选项。这样做时,有时用户会遇到此错误。有没有办法增加apache中URL的长度?

回答by John Feminella

Under Apache, the limit is a configurable value, LimitRequestLine. Change this value to something larger than its default of 8190 if you want to support a longer request URI. The value is in /etc/apache2/apache2.conf. If not, add a new line (LimitRequestLine 10000) under AccessFileName .htaccess.

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

However, note that if you're actually running into this limit, you are probably abusing GETto begin with. You should use POSTto transmit this sort of data -- especially since you even concede that you're using it to update values. If you check the link above, you'll notice that Apache even says "Under normal conditions, the value should not be changed from the default."

但是,请注意,如果您确实遇到了此限制,那么您可能一GET开始就在滥用。您应该使用POST来传输此类数据——尤其是因为您甚至承认您正在使用它来更新值。如果您查看上面的链接,您会注意到 Apache 甚至说“在正常情况下,不应更改默认值”。

回答by atmelino

Based on John's answer, I changed the GET request to a POST request. It works, without having to change the server configuration. So I went looking how to implement this. The following pages were helpful:

根据约翰的回答,我将 GET 请求更改为 POST 请求。它可以工作,而无需更改服务器配置。所以我去寻找如何实现这一点。以下页面很有帮助:

jQuery Ajax POST example with PHP(Note the sanitize posted data remark) and

带有 PHP 的 jQuery Ajax POST 示例(注意清理发布的数据备注)和

http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

Basically, the difference is that the GET request has the url and parameters in one string and then sends null:

基本上,区别在于GET请求将url和参数放在一个字符串中,然后发送null:

http.open("GET", url+"?"+params, true);
http.send(null);

whereas the POST request sends the url and the parameters in separate commands:

而 POST 请求在单独的命令中发送 url 和参数:

http.open("POST", url, true);
http.send(params);

Here is a working example:

这是一个工作示例:

ajaxPOST.html:

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:

ajaxPOST.php:

<?php

$lorem=$_POST['lorem'];
print $lorem.'<br>';

?>

I just sent over 12,000 characters without any problems.

我刚刚发送了超过 12,000 个字符,没有任何问题。

回答by Fusca Software

I got this error after using $.getJSON() from JQuery. I just changed to post:

使用 JQuery 中的 $.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!");
    });

回答by Shrey Gupta

I have a simple workaround.

我有一个简单的解决方法。

Suppose your URI has a string stringdatathat is too long. You can simply break it into a number of parts depending on the limits of your server. Then submit the first one, in my case to write a file. Then submit the next ones to append to previously added data.

假设您的 URI 有一个stringdata太长的字符串。您可以根据服务器的限制将其简单地分成多个部分。然后提交第一个,在我的情况下写一个文件。然后提交下一个附加到先前添加的数据。

回答by Your Common Sense

An excerpt from the RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1:

RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1的摘录

The POSTmethod is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line. POST is designed to allow a uniform method to cover the following functions:

  • Annotation of existing resources;
  • Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles;
  • Providing a block of data, such as the result of submitting a form, to a data-handling process;
  • Extending a database through an append operation.

POST方法用来请求原始服务器接受被附在请求由请求URI中使用Request-Line标识的资源的新下属的实体。POST 旨在允许一个统一的方法来覆盖以下功能:

  • 现有资源的注释;
  • 向公告板、新闻组、邮件列表或类似的文章组发布消息;
  • 向数据处理过程提供数据块,例如提交表单的结果
  • 通过追加操作扩展数据库。