all*_*ded 5 javascript xmlhttprequest
尝试使用纯JS方法检查我是否具有有效的JS图像网址。我收到XMLHttpRequest
不建议使用的警告。有什么更好的方法可以做到这一点?
urlExists(url) {
const http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
if (http.status !== 404) {
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
您可能会收到一条消息,提示已弃用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()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6705 次 |
最近记录: |