开发者

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: " + r开发者_如何转开发equest.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";
?>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜