我请求一个简单的 php 文件来回显一行;使用 AJAX,但我从 Linux 上的本地主机服务器收到 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();
test.php:
<?php echo 'this is a test'; ?>
最佳答案
据了解,在readyState
之前你无法检测服务器的状态。如果是 4
(即 DONE
),则可以检查服务器状态。
因此,在检查status
之前,您需要先检查readyState
。
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
关于php - Ajax返回HTTP响应501 : method not implemented,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14447086/