2013-01-21 64 views
0

我正在請求一個簡單的PHP文件,回聲單線;使用AJAX,但我從Linux上的本地主機服務器獲得HTTP 501響應。我的問題是,什麼會導致這種錯誤?Ajax返回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'; ?> 
+10

可能你的代碼,你沒有張貼。 – Musa

+1

http://www.checkupdown.com/status/E501.html – epascarello

+0

很難說沒有看到您的代碼。我最近在IIS服務器上使用'PUT'和'DELETE'時遇到了類似的問題,因爲默認情況下這些動詞是不允許的。您可能會遇到類似的行爲,因爲我不認爲apache(我認爲您可能正在使用它)支持這些自動啓用的動詞。 – War10ck

回答

0

至於所知,你不能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