[英]How to fetch (using Fetch API) only headers in Javascript, not the whole content?
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.在这里,我有这个示例小提琴,我尝试在 Javascript 中同时使用
XMLHttpRequest()
和fetch()
从大图像中仅获取content-type
标头。 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.如果您在 Chrome 的开发人员工具/网络选项卡(或其他浏览器中的类似工具)打开的情况下运行小提琴,您将看到虽然
XMLHttpRequest()
方法仅获取标头所在的 600 B,但fetch()
方法获取整个 7.8 MB 图像,尽管我的代码只需要content-type
标头。
How can I get just those few bytes needed for the headers (instead of the whole content) using the Fetch APi's fetch()
?我怎样才能使用 Fetch API 的
fetch()
获得标题(而不是整个内容)所需的那几个字节?
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.注意:我对此很感兴趣,因为显然,如果我尝试获取常规 Google 页面的所有链接的标题,
XMLHttpRequest()
方法会将我注销,有时也会更改我的语言,而fetch()
方法才不是。 I assume this is because the former sends the credentials / cookies / etc., while the latter is more 'restrictive' by default...我认为这是因为前者发送凭据/cookies/等,而后者默认情况下更具“限制性”......
You need to specify the method type of HEAD
:您需要指定
HEAD
的方法类型:
fetch(url, {method: 'HEAD'})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.