Q:

PHP returning status 0

I am having a problem with AJAX, where the PHP returns a status code of 0. I am running it on a local domain, and I don't see why it wont return anything. It does write to bar.txt, but it does not return anything to the AJAX request, and the status code is 0.
Ajax request:
function sendRequestToButler(requestText, callback) {
  try {
    var request = new XMLHttpRequest();
    request.onreadystatechange = function() {if(request.readyState == 4 && request.status == 200) {callback(request);} else {alert("Error: AJAX: readyState: " + request.readyState + " status: " + request.status);}};
    request.open("POST", "foo.php", true);
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.setRequestHeader("Content-length", requestText.length);
    request.setRequestHeader("Connection", "close");
    request.send(request);
  } catch(e) {alert("error"); return "CouldNotCompleteRequest";}
}

foo.php:
<?php
  file_put_contents('bar.txt', "facepalm", LOCK_EX);
  echo "foobar";
?>

A:

the basis of AJAX is that it is Asynchronous Javascript And Xml. Your server does not have time to respond before the javascript moves on to the next statement. The status is zero because nothing has happened yet. You either need to wait for the response synchronously to have a callback method that displays the response once it is done. Also, it isn't an error if the readystate changes and it's not 4.

