How to fetch (using Fetch API) only headers in Javascript, not the whole content?(如何仅获取(使用 Fetch API)Javascript 中的标头,而不是全部内容?)
问题描述
这里我有这个小提琴样本,我尝试只获得 大图像中的 content-type
标头,在 Javascript 中同时使用 XMLHttpRequest()
和 fetch()
.如果您在 Chrome 的开发者工具/网络选项卡(或其他浏览器中的类似工具)打开的情况下运行 fiddle,您会看到虽然 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()
方法没有.我认为这是因为前者发送凭据/cookies等,而后者默认情况下更具限制性"......
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
的方法类型:
fetch(url, {method: 'HEAD'})
查看更新的小提琴
这篇关于如何仅获取(使用 Fetch API)Javascript 中的标头,而不是全部内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何仅获取(使用 Fetch API)Javascript 中的标头,而不是全部内容?


- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 400或500级别的HTTP响应 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01