0

我正在请求一个回显一行的简单 php 文件;使用 AJAX,但我从 linux 上的 localhost 服务器收到 HTTP 501 响应。我的问题是,什么可能导致这种错误?

JavaScript:

var request = new XMLHttpRequest();

request.onreadystatechange = function () {
    //console.log(request.responseText);
    if (request.readyState == 4 && request.status == 200) {
        console.log(0);
    } else if (request.status == 501) {
        console.log(request.responseText);
    }
}
request.open('test.php', 'GET', true);
request.send();

测试.php:

<?php echo 'this is a test'; ?>
4

1 回答 1

0

据了解,您无法检测到服务器之前的状态readyState。如果是4(so DONE) 则可以检查服务器状态。

所以你需要在检查readyState之前先检查status

if (req.readyState === 4) {
    // 200 is not enough to detect successful requests
    // cos there are many success status codes
    if (req.status === 200) {
        // every thing ok
    } else {
        // something is wrong with server?
    }
}

在这里查看 HTTP 状态代码:http: //www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

也许这对你有用:https ://github.com/qeremy/mii/blob/master/mii.ajax.js#L248

于 2013-01-22T04:17:03.470 回答