我相信這是比較接近你想找的 - 參照上NETTUTS香草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('');
}
,你可以建立自己的「成功」處理程序放到基於「保障準備就緒」返回值
heres a link to the nettuts tutorial
and another to the MDN ajax API docs
來源
2013-10-28 18:44:42
Joe
這裏是一個jQuery的例子,你只需要取消它的jQuery :http://stackoverflow.com/a/333884/600135 – kol
我其實已經檢查了這個線程。但問題是,我不能「un-jQuery」它,這就是爲什麼我要求一個純粹的JS解決方案:-)你看,jQuery的「成功:」部分可能是我正在尋找的。但我不知道沒有jQuery,看起來像什麼 –
如果人們有了它們,仍然需要一些答案。 –