Check url content type with javascript

柔情痞子 提交于 2019-12-07 16:36:31

问题


In order to conserve server resources I'm looking for a way to retrieve the content type of a given url using javascript. It should not download the complete content from the url only the headers. Is this possible with the restrictions javascript has.


回答1:


Make an Ajax call with a head request.

var url = window.location.href;
var xhttp = new XMLHttpRequest();
xhttp.open('HEAD', url);
xhttp.onreadystatechange = function () {
    if (this.readyState == this.DONE) {
        console.log(this.status);
        console.log(this.getResponseHeader("Content-Type"));
    }
};
xhttp.send();


来源:https://stackoverflow.com/questions/24050112/check-url-content-type-with-javascript

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