如果没有JQuery,怎么办呢?
这是我的代码:
function runScript(params) {
xhr = new XMLHttpRequest();
xhr.open('POST', 'scripts.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
if (xhr.status === 200) {
document.getElementById("dynamic").innerHTML = xhr.responseText;
}
else {
alert('Request failed. Returned status of ' + xhr.status);
}
};
xhr.send(encodeURI(params));
}
我想要这样的事情:
xhr.finished {
alert(AJAX has finished it's process and the HTML has been updated);
}
答案 0 :(得分:0)
您正在附加错误的事件处理程序...尝试onreadystatechange
。对于您的情况,我建议您在runScript
函数中添加回调函数:
function runScript(params, callback) {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'scripts.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if(xhr.readyState === XMLHttpRequest.DONE) {
callback(xhr.status, xhr.responseText);
}
};
xhr.send(encodeURI(params));
}
runScript('foo=bar', function finished(status, response) {
console.log('It is done...');
});