如何在 Firefox 中强制或添加 ajax 类型 POST 请求的内容长度?

发布于 2024-07-26 00:41:55 字数 1160 浏览 1 评论 0原文

我正在尝试使用 ajax 发布 http 请求,但使用 modsec_audit 从 apache 服务器获取响应:“POST 请求必须具有 Content-Length 标头。” 我不想在 modsec_audit 中禁用此功能。

这种情况只发生在 Firefox 中,而不会发生在 IE 中。 此外,我改用 POST 而不是 GET 来防止 IE 缓存我的结果。

这是我用于请求的代码的简化版本,我没有使用任何 JavaScript 框架。

function getMyStuff(){
    var SearchString = '';
    /* build search string */
    ...
    /* now do request */
    var xhr = createXMLHttpRequest();
    var RequestString = 'someserverscript.cfm' + SearchString;
    xhr.open("POST", RequestString, true);
    xhr.onreadystatechange = function(){
        processResponse(xhr);
    }
    xhr.send(null);
}


function processResponse(xhr){
    var serverResponse = xhr.responseText;
    var container = document.getElementById('myResultsContainer');
    if (xhr.readyState == 4){
        container.innerHTML = serverResponse;
    }
}

function createXMLHttpRequest(){
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
    try { return new XMLHttpRequest(); } catch(e) {}
    return null;
}

如何在 Firefox 中强制或添加 ajax 类型 POST 请求的内容长度?

I'm trying to POST a http request using ajax, but getting a response from the apache server using modsec_audit that: "POST request must have a Content-Length header." I do not want to disable this in modsec_audit.

This occurs only in firefox, and not IE. Further, I switched to using a POST rather than a GET to keep IE from caching my results.

This is a simplified version of the code I'm using for the request, I'm not using any javascript framework.

function getMyStuff(){
    var SearchString = '';
    /* build search string */
    ...
    /* now do request */
    var xhr = createXMLHttpRequest();
    var RequestString = 'someserverscript.cfm' + SearchString;
    xhr.open("POST", RequestString, true);
    xhr.onreadystatechange = function(){
        processResponse(xhr);
    }
    xhr.send(null);
}


function processResponse(xhr){
    var serverResponse = xhr.responseText;
    var container = document.getElementById('myResultsContainer');
    if (xhr.readyState == 4){
        container.innerHTML = serverResponse;
    }
}

function createXMLHttpRequest(){
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
    try { return new XMLHttpRequest(); } catch(e) {}
    return null;
}

How do I force or add the content length for ajax type POST requests in Firefox?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮色兮凉城 2024-08-02 00:41:55
xhr.setRequestHeader("Content-Length", "0");

这是我最好的猜测。

顺便说一句,如果你想在 IE 中停止缓存,只需在末尾添加一个随机数,如下所示:

var RequestString = 'someserverscript.cfm' + SearchString + '&random=' + Math.random();
xhr.setRequestHeader("Content-Length", "0");

would be my best guess.

BTW, if you want to stop caching in IE, just add a random number onto the end, as in:

var RequestString = 'someserverscript.cfm' + SearchString + '&random=' + Math.random();
傾城如夢未必闌珊 2024-08-02 00:41:55

尝试实际发送一些内容而不是 null (xhr.send(null);)。

Try to actually send something instead of null (xhr.send(null);).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文