如何仅使用Javascript中的标头而不是整个内容来获取(使用Fetch API)? [英] How to fetch (using Fetch API) only headers in Javascript, not the whole content?
问题描述
在这里,我有这个样本小提琴,在这里我尝试仅获取 >大图像中的content-type
标头,在Javascript中同时使用XMLHttpRequest()
和fetch()
.如果在打开Chrome的开发人员工具/网络"标签(或其他浏览器中的类似工具)的情况下运行小提琴,则会看到,虽然XMLHttpRequest()
方法仅获得标头所在的600 B,但fetch()
方法却获得了整个7.8 MB的图片,尽管我的代码只需要content-type
标头即可.
Here I have this sample fiddle, where I try to get only the content-type
header from a large image, using both XMLHttpRequest()
and fetch()
in Javascript. If you run the fiddle with Chrome's Developer Tools / Network tab (or similar tools in other browsers) open, you'll see that while the XMLHttpRequest()
method gets only the 600 B where the headers are located, the fetch()
method gets the whole 7.8 MB image, despite my code only needing the content-type
headers.
如何使用Fetch APi的fetch()
获得标头所需的几个字节(而不是整个内容)?
How can I get just those few bytes needed for the headers (instead of the whole content) using the Fetch APi's fetch()
?
注意:我对此感兴趣,因为显然,如果我尝试获取常规Google页面的所有链接的标题,则XMLHttpRequest()
方法会使我注销,有时还会更改我的语言,而fetch()
方法没有.我认为这是因为前者发送凭据/Cookie等,而后者默认情况下更具限制性" ...
Note: I am interested in this, because apparently, if I try to get the headers of all the links of a regular Google page, the XMLHttpRequest()
method logs me out and sometimes changes my language as well, while the fetch()
method does not. I assume this is because the former sends the credentials / cookies / etc., while the latter is more 'restrictive' by default...
推荐答案
您需要指定HEAD
的方法类型:
You need to specify the method type of HEAD
:
fetch(url, {method: 'HEAD'})
这篇关于如何仅使用Javascript中的标头而不是整个内容来获取(使用Fetch API)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!