Search code examples
ajaxapachepostxmlhttprequestpostdata

AJAX postdata working on localhost but not on (Apache) server


I identified an issue with postdata not being sent through AJAX on my server. To debug it, I wrote the following fairly minimalistic piece of javascript to test a simple AJAX call :

function my_custom_ajax(target_page, target_element , postdata_contents) {

  // Sending the XMLHttpRequest as postdata
  var xhr = new XMLHttpRequest();
  xhr.open("POST", target_page, true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
  xhr.setRequestHeader("Content-length", postdata_contents.length);
  xhr.setRequestHeader("Connection", "close");
  xhr.send(postdata_contents);

  // Waiting for the request to return
  xhr.onreadystatechange = return_data;

  // If all went well, we update the target element
  function return_data()
  {
    if(xhr.readyState === 4)
    {
      if(xhr.status === 200)
      {
        // We update the target element
        document.getElementById(target_element).innerHTML = xhr.responseText;
      }
      // Throw error in case of 404 or such
      else
        document.getElementById(target_element).innerHTML = "XHR can't be loaded";
    }
    // Throw error in case request got interrupted or didn't work out
    else
      document.getElementById(target_element).innerHTML = "XHR error";
  }
}

It is called with the following HTML :

<div onClick="my_custom_ajax('test_page.php?xhr','my_id','postdata_test');">
  Click me
</div>
<div id="my_id">
  xhr response will appear here
</div>

And calls a PHP page which contains only this :

exit(var_dump($_POST));

When running this piece of code in my Apache localhost or another Apache server I own, it does pass whatever is in postdata_contents as postdata. The exit(var_dump($_POST)); does show that it works properly, and does print the value of the postdata I passed to it.

However, when running this same piece of code on the Apache server where it does not work, all I get is « array(0) { } », as in, no postdata is passed according to the PHP file.

Here is Firefox's dev tool view of the request details (in french, sorry, but should be obvious what is what) :

The debug tool shows that the postdata contents are properly being sent :

However, the returned content show that the postdata was somehow not passed :

On my localhost and my other Apache server, everything is exactly the same until the very last step, where the postdata is properly passed (the var_dump message is styled but you can easily see the gist of it : postdata_test is part of $_POST) :

After hours of fiddling with the configuration of this Apache server and trying all of the debug methods and breakpoints I could think up, my nerves are too worked up to continue thinking about this rationally for now. As I have no option of using another server or just copypasting my local Apache configuration file on the new server, I defer this question to you all, hoping that somebody can figure it out or once encountered something similar.

Thanks in advance, Eric B.


Solution

  • Solved it myself by accident, I had mod_dumpio activated on the server and it started working once I turned it off.

    I do not know what mod_dumpio was doing to deny XHR POST but not generic HTTP POST, but at least that's solved.

    Hope this will help someone else some day.

    (on a sidenote, I realize the postdata query in my example was malformed, should have been « postdata_test= » instead of « postdata_test », so add that equal sign if you are stuck in my situation and want to run the same tests I did)