尝试使用纯 JS 方法来检查我是否有有效的 JS 图像 url。我收到警告XMLHttpRequest
已弃用。有什么更好的方法来做到这一点?
urlExists(url) {
const http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
if (http.status !== 404) {
return true;
}
return false;
}
您可能会收到一条消息,表明同步使用XMLHttpRequest 已被弃用(因为它对用户体验产生有害影响;它在等待响应时冻结页面)。我可以向你保证,正确的异步无论如何,该 API 的使用并未被弃用。
以下是正确使用的一些示例代码:
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function() {
if (this.readyState === this.DONE) {
console.log(this.status) // do something; the request has completed
}
}
xhr.open("HEAD", "http://example.com") // replace with URL of your choosing
xhr.send()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)