【问题标题】:Ajax returning HTTP response 501: method not implementedAjax 返回 HTTP 响应 501:方法未实现
【发布时间】:2013-01-21 20:57:20
【问题描述】:

我正在请求一个简单的 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();

test.php:

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

【问题讨论】:

  • 可能是您的代码,但您没有发布。
  • 很难在没有看到您的代码的情况下判断。我最近在 IIS 服务器上使用 PUT 和 DELETE 时遇到了类似的问题,因为默认情况下不允许使用这些动词。您可能会遇到类似的行为,因为我认为 apache(我认为您可能正在使用)也不支持自动启用这些动词。
  • @Musa 抱歉,我现在发布了...

标签: php javascript ajax linux apache


【解决方案1】:

据了解,readyState之前的服务器状态是无法检测到的。如果是4(所以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

【讨论】:

    猜你喜欢
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多