最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin7浏览0评论

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?

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?

Share Improve this question asked Jun 30, 2009 at 20:09 JaysonJayson 2,0314 gold badges25 silver badges26 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4
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();

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

发布评论

评论列表(0)

  1. 暂无评论