从 XHR 获取响应 Content-Type 标头

我想看看标题是 text/html 还是 text/xml.如果它是 text/html 则有一个错误,我宁愿在继续之前抓住它.

I would like to see whether the header was text/html or text/xml. If it was text/html then there was an error and I would rather catch that before proceeding.

推荐答案

使用 getResponseHeader() 方法.

小例子:

<script>
function hand () {
        console.log(this.getResponseHeader('content-type'));
}
var x = new XMLHttpRequest();
x.onreadystatechange = hand;
x.open('GET', 'index.html', true);
x.send();
</script>

相关文章