Javascript/Ajax 中的 HTTP HEAD 请求?
是否可以仅使用 JavaScript 中的 XMLHTTPRequest 来执行 HTTP Head 请求?
我的动机是节省带宽。
如果不是,是否可以造假?
Is it possible to do a HTTP Head request solely using an XMLHTTPRequest in JavaScript?
My motivation is to conserve bandwidth.
If not, is it possible to fake it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很简单,只需使用 HEAD 方法,而不是 GET 或 POST:
这只是一个简短的示例,展示如何使用 HEAD 方法。 生产代码可能需要针对不同结果状态(成功、失败、超时)进行更细粒度的回调,并且可能使用不同的 事件处理程序(
onload
、onerror
和ontimeout
而不是onreadystatechange
)。Easy, just use the HEAD method, instead of GET or POST:
This is just a short example to show how to use the HEAD method. Production code may need more fine-grained callbacks for different result states (success, failure, timeout), and may use different event handlers (
onload
,onerror
andontimeout
rather thanonreadystatechange
).更现代的方法是使用 Fetch API 替换
XMLHttpRequest
。例如(在
async
函数内)The more modern approach is to use the Fetch API which replaced
XMLHttpRequest
.e.g. (within an
async
function)XMLHTTPRequest 对象
应该在其上定义
An XMLHTTPRequest object should have
defined on it