是否可能仅使用JavaScript中的XMLHTTPRequest进行HTTP Head请求?
我的动机是为了节省带宽。
如果不行,是否可以伪造实现?
是否可能仅使用JavaScript中的XMLHTTPRequest进行HTTP Head请求?
我的动机是为了节省带宽。
如果不行,是否可以伪造实现?
很简单,只需使用HEAD方法,而不是GET或POST:
function UrlExists(url, callback)
{
var http = new XMLHttpRequest();
http.open('HEAD', url);
http.onreadystatechange = function() {
if (this.readyState == this.DONE) {
callback(this.status != 404);
}
};
http.send();
}
这只是一个简单的示例,展示如何使用HEAD方法。生产代码可能需要更精细的回调来处理不同的结果状态(成功、失败、超时),并且可能会使用不同的事件处理程序(onload
、onerror
和ontimeout
而不是onreadystatechange
)。
XMLHttpRequest
。async
函数内部):const url = "https://example.com";
const response = await fetch(url, { method: "HEAD" });
console.log(`Got status: ${response.status}`);
一个XMLHTTPRequest对象应该具备以下特点
getAllResponseHeaders();
getResponseHeader("header-name")
在其中定义