如何重新发送相同的ajax请求,直到页面结束时使用纯javascript(请不要使用框架)。按照我的代码:
xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","demo_post2.asp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
// resend the same request, until the close of the page
}
答案 0 :(得分:1)
我不知道你为什么要这样做,但是你走了。
// Reusing variable to keep garbage low.
var xhr;
// Recursive request.
function recursiveXHR () {
xhr = new XMLHttpRequest();
xhr.open('POST','demo_post2.asp',true);
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
// This is where the method is called again once the previous request is done.
xhr.addEventListener('load', recursiveXHR, false);
xhr.send('fname=Henry&lname=Ford');
}
// Initializing the recursive calling.
recursiveXHR();