如何使用javascript获取URL HTTP状态代码403

时间:2015-07-21 09:32:18

标签: javascript ajax http-status-code-403

我有以下javascript代码,我想通过使用javascript获取HTTP 403 Forbidden响应代码,我尝试使用以下代码并对其进行修改,但它不适用于我..

<script type="text/javascript">

var request = new XMLHttpRequest();  
request.open('GET', 'https://domain.com/file', true);
request.send();
request.onreadystatechange = function(){

        if (request.status === 403) {  
            alert("Oh no, it does not exist!");
        }
};



</script>

1 个答案:

答案 0 :(得分:0)

首先,我会尝试添加readyState支票,如下所示:

var request = new XMLHttpRequest();  
request.open('GET', 'https://domain.com/file', true);
request.onreadystatechange = function(){
    if (request.readyState === 4) {
        if (request.status === 403) {  
            alert("Oh no, it does not exist!");
        }
    }
};
request.send();

这可以解决吗?