我相信这与您正在寻找的内容相对接近 - 参考 nettuts plus 上的 vanilla js ajax 教程:
load('test.txt', function(xhr) {
document.getElementById('container').innerHTML = xhr.responseText;
// you would obviously custom tailor this peice to utilize your specific data
});
function load(url, callback) {
var xhr;
if(typeof XMLHttpRequest !== 'undefined') xhr = new XMLHttpRequest();
else {
var versions = ["MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
"Microsoft.XmlHttp"]
for(var i = 0, len = versions.length; i < len; i++) {
try {
xhr = new ActiveXObject(versions[i]);
break;
}
catch(e){}
} // end for
}
xhr.onreadystatechange = ensureReadiness;
function ensureReadiness() {
if(xhr.readyState < 4) {
return;
}
if(xhr.status !== 200) {
return;
}
// all is well
if(xhr.readyState === 4) {
callback(xhr);
}
}
xhr.open('GET', url, true);
xhr.send('');
}
您可以根据返回值将“成功”处理程序构建到“确保准备就绪”中
这是 nettuts 教程的链接
另一个是 MDN ajax API 文档