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 中的标头,而不是全部内容?


基础教程推荐
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01