简体   繁体   中英

How to get urls HTTP status code 403 with javascript

i have following javascript code, i want to get HTTP 403 Forbidden response code by using javascript, i tried following code and modified it, but its not working for me..

<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>

First, I would try adding the readyState check, like so:

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();

Does this fix it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM