使用POST方法的AJAX函数会创建以下错误.错误:返回状态码414请求uri太大

AJAX function that uses the POST method creates the following error. Error: returned status code 414 Request-URI Too Large

本文关键字:错误 状态 返回 太大 uri 请求 方法 POST AJAX 函数 创建      更新时间:2023-09-26

我使用AJAX函数将数据传输到PHP文件。我传递给AJAX函数的数据有17000个字符长。这对于使用GET方法传输来说通常太长了,但是人们会认为POST方法会允许传递这么大的变量。

下面是我使用的AJAX函数:
function ajaxFunction(id, datatypeString, pathToFileString, variable){
    var myRequestObject = null; 
    document.getElementById(id).innerHTML = "<span>Started...</span>";
    if (window.XMLHttpRequest)
    {
        myRequestObject = new XMLHttpRequest();
    } 
    else if (window.ActiveXObject) 
    {
        try 
        {
            myRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e)
        {
            try 
            {
                myRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
        }
    }

    myRequestObject.onreadystatechange = function()
    { 
        document.getElementById(id).innerHTML = "<span>Wait server...</span>";
        if(myRequestObject.readyState == 4)
        {
            if(myRequestObject.status == 200)
            {
                // process a document here
                document.getElementById(id).innerHTML = "<span>Processing file...</span>"
                if(datatypeString == "txt"){
                    //Injects code from a text file
                    document.getElementById(id).innerHTML = myRequestObject.responseText;
                }
                else if(datatypeString == "xml"){
                    //Injects code from an XML file
                    document.getElementById(id).innerHTML = myRequestObject.responseXML.documentElement.document.getElementsByTagName('title')[0].childNodes[0].nodeValue;   // Inject the content into the div with the relevant id 
                }
                else{
                    document.getElementById(id).innerHTML = "<span>Datatype exception occured</span>";
                }
            }   
            else    
            {
                document.getElementById(id).innerHTML = "<span>Error: returned status code " + myRequestObject.status + " " + myRequestObject.statusText + "</span>";
            }   
        } 
    }; 
    myRequestObject.open("POST", pathToFileString+variable, true); 
    myRequestObject.send(null); 
}

这是对AJAX函数的函数调用:

ajaxFunction("myDiv", "txt", "processdata.php", "?data="+reallyLargeJavascriptVariable);

这也是当AJAX函数被调用时我得到的错误:

Error: returned status code 414 Request-URI Too Large

我在Stackoverflow和其他网站上寻找解决这个问题的方法。然而,大多数答案归结为:"使用POST方法而不是GET方法来传输数据。"

但是,正如您在AJAX函数中看到的,我已经使用了POST方法。

所以我不确定这里发生了什么,以及在我的代码中改变什么来解决这个问题。我只是想把很大的变量传递给我的函数,但是对于这个函数,这似乎是不可能的。

给出错误,似乎是URI的限制导致了问题。但是,我使用的是POST方法而不是GET方法,那么为什么变量仍然通过URI传递呢?由于我没有使用GET方法,而是使用POST方法,就像许多人在其他线程中建议的那样,因此我不确定为什么这里涉及URI并且似乎会引起问题。

显然,URI是把我可以传输的变量的大小限制,但我使用POST方法,所以为什么会发生这种错误,我怎么能调整我的AJAX函数,使其与我想使用AJAX传输的大变量的工作?

当你做一个POST你需要传递的.send POST数据(你目前传递null)。您还需要设置一些标题细节。

myRequestObject.open("POST", pathToFileString, true);
myRequestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
myRequestObject.setRequestHeader("Content-length", variable.length);
myRequestObject.send(variable);

如果你当前在变量的开头或路径的结尾传递一个问号,请继续并删除它