HTTP Status Code from URL in Javascript

懵懂的女人 提交于 2020-05-27 12:39:32

问题


I'm trying to find a function that return HTTP Status Code from an URL, and then "make something" based on the returned statuses (404, 416, 200 etc...)

Can someone help me? I've tried the other functions posted here on StackOverflow but anyone was usefull for my purpose.

I need to integrate this function inside my PlayFramework web-app.

Thanks a lot


回答1:


Can you try the following ..?

function getStatus(url) {
        var request = new XMLHttpRequest();
    request.onreadystatechange = function() {
        if (request.readyState === 4){
            request.status;//this contains the status code
        }
    };
request.open("GET", url , true);
request.send(null);
}


来源:https://stackoverflow.com/questions/29097819/http-status-code-from-url-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!