INVALID_STATE_ERR:DOM 异常 11
好的,我已经创建了一个有效的 javascript ajax 文件,但它生成了数量惊人的 dom 异常。我不确定为什么会这样,因为从我所看到的来看,我调用的所有元素目前仍然存在。 代码在这里:
window.onload = function(){init();}
function init() {
ajax = ajaxInit();
setInterval(function(){ajaxContact(ajax);},2000);
ajaxContact(ajax);
ajax.onreadystatechange = function() {update(ajax);}
}
function ajaxInit() {
if (window.XMLHttpRequest) {
ajax = new XMLHttpRequest();
}
else {
if (window.ActiveXObject) {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
}
if (ajax) {
document.getElementById("status").innerHTML = "AJAX initialized";
return ajax;
}
else {
docuement.getElementById("status").innerHTML = "Error: AJAX not available";
return false;
}
}
function ajaxContact(ajax) {
try {
ajax.open("GET","updateAjax.php?" + "ran=" + Math.random(),true);
ajax.send();
}
catch (err) {
alert(err.message);
document.getElementById("status").innerHTML = "Error contacting server";
document.getElementById("loading").src = "images/redx.png";
}
}
function update(ajax) {
if (ajax.readyState==4 && ajax.status==200){
dataObj = eval('(' + ajax.responseText + ')');
document.getElementById("status").innerHTML = dataObj.status;
document.getElementById("frameNumber").innerHTML =
"Frame:" + dataObj.firstFrame + "/" + dataObj.lastFrame;
document.getElementById("thumbnail").src = dataObj.imgSrc;
}
if (ajax.status==404) {
document.getElementById("status").innerHTML = "Ajax updater not found";
document.getElementById("loading").src = "images/redx.png";
}
}
Ok, I've created a working javascript ajax file, but it generates an absurd number of these dom exceptions. I'm not sure why that is, because from what I can see, all the elements I call are currently still in existance.
The code is here:
window.onload = function(){init();}
function init() {
ajax = ajaxInit();
setInterval(function(){ajaxContact(ajax);},2000);
ajaxContact(ajax);
ajax.onreadystatechange = function() {update(ajax);}
}
function ajaxInit() {
if (window.XMLHttpRequest) {
ajax = new XMLHttpRequest();
}
else {
if (window.ActiveXObject) {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
}
if (ajax) {
document.getElementById("status").innerHTML = "AJAX initialized";
return ajax;
}
else {
docuement.getElementById("status").innerHTML = "Error: AJAX not available";
return false;
}
}
function ajaxContact(ajax) {
try {
ajax.open("GET","updateAjax.php?" + "ran=" + Math.random(),true);
ajax.send();
}
catch (err) {
alert(err.message);
document.getElementById("status").innerHTML = "Error contacting server";
document.getElementById("loading").src = "images/redx.png";
}
}
function update(ajax) {
if (ajax.readyState==4 && ajax.status==200){
dataObj = eval('(' + ajax.responseText + ')');
document.getElementById("status").innerHTML = dataObj.status;
document.getElementById("frameNumber").innerHTML =
"Frame:" + dataObj.firstFrame + "/" + dataObj.lastFrame;
document.getElementById("thumbnail").src = dataObj.imgSrc;
}
if (ajax.status==404) {
document.getElementById("status").innerHTML = "Ajax updater not found";
document.getElementById("loading").src = "images/redx.png";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
<罢工>
您可能尝试在
ajax
上调用 open 和 send,但如果请求在setInterval
每次调用之间的两秒内未完成,则会抛出错误。您需要在每次调用中检查ajax
对象是否已发送或准备好打开(检查ajax.readyState
)。在 Chrome 中,该行
通过访问
ajax.readyState
为HEADERS_RECEIVED
(2)、LOADING
(3 ),完成
(4)。 在访问状态之前尝试确保对象已准备好。
You are probably trying to call open and send on
ajax
, but it throws errors if the request has not finished within the two seconds between each call bysetInterval
. You need to check in each call whether theajax
object has been sent already or is ready for opening (checkajax.readyState
).In Chrome, the line
causes the error by accessing the status before
ajax.readyState
isHEADERS_RECEIVED
(2),LOADING
(3),DONE
(4). Try making itto make sure that the object is ready before accessing the status.