views:

689

answers:

3

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?

+3  A: 
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();
MiffTheFox
man, that was too easy; I should have figured that one out myself. setRequestHeader() doesn't work properly though unless you replace null with '' in send().
Jayson
A: 

Not an answer to this question, but related to it, I asked "Why does modsecurity require Content-Length in POST requests?" over at serverfault.

toolbear74
A: 

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

Alex Polo