【发布时间】:2011-10-24 02:10:12
【问题描述】:
我喜欢在没有 jquery 的情况下做尽可能多的代码,所以对于 ajax 请求,我一直在按照 MDN 所说的做一些事情:
function alertContents(httpRequest) {
try {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
alert(httpRequest.responseText);
} else {
alert('There was a problem with the request.');
}
}
}
catch( e ) {
alert('Caught Exception: ' + e.description);
}
}
我正在查看 Google 制作扩展的教程,他们使用了他们请求的 onload。 onload 是在 readystate 为 4 且状态为 200 时运行的事件监听吗?如果不是,它是什么,我什么时候用它来代替上面的方法。
【问题讨论】:
-
为什么不实现它,看看它什么时候触发?我要试一试
-
许多“ajax 包装器”在
readyState === 4上调用成功。试一试这个页面,它展示了一个很好的AJAX包装器示例,其中包含一个onSuccess方法:ibm.com/developerworks/library/wa-aj-ajaxhistory/index.html
标签: javascript ajax xmlhttprequest