javascript - 如何知道,AJAX 请求何时在 Native Javascript 中完全加载?
问题描述
这是我使用 setTimeout 进行的 AJAX 调用
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
document.getElementById('mainContent').style.display = 'none';
if (this.readyState == 4 && this.status == 200) {
xhttp.onload = function(){
document.getElementById("mainContent").innerHTML = this.responseText;
setTimeout(function(
document.getElementById("mainContent").style.display = 'block';
),1000);
}
}
};
xhttp.open("POST", "system.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("lang="+lang);
我想检测ajax何时完全加载,我也有解决方案
我在这里有解决方案,但我不明白它是如何工作的。
var oldOpen = XMLHttpRequest.prototype.open;
function onStateChange(event) {
// fires on every readystatechange ever
// use `this` to determine which XHR object fired the change event
}
XMLHttpRequest.prototype.open = function() {
// when an XHR object is opened, add a listener for its readystatechange events
this.addEventListener("readystatechange", onStateChange)
// run the real `open`
oldOpen.apply(this, arguments);
}
你能举例说明它是如何工作的或编写完整的代码它是如何工作的吗?
解决方案
推荐阅读
- snowflake-cloud-data-platform - 能否同时使用 Azure Active Directory 和 Okta 进行 SSO
- swift - 使用 GeoFire 将位置保存到特定的孩子
- c# - 将 System.IO.Abstractions 与 DirectoryInfo 一起使用
- python - Python 替换文字美汤
- javascript - 异步 AWS Lambda 函数混淆
- python - 创建定义比例的随机训练测试拆分,同时保持每个集合中一个属性的排他性
- passwords - Geoserver - PostGIS 数据库连接问题 - 默认密码总是出现
- html - Ag Grid Column 展开问题
- python - 使用 Fitz 在 Python 中替换 pdf 文件中的文本
- java - 如何安装 glassfish server 5 并在远程主机上部署 war 文件