简体   繁体   English

完成上一个请求后如何重新发送Ajax请求

[英]How to resend an Ajax request, after completing the previous one

I am trying execuiting a simple Ajax request to a web page (triggered by a onclick event on a button): 我正在尝试对网页执行简单的Ajax请求(由按钮上的onclick事件触发):

    // 1. crea un oggetto di tipo XMLHttpRequest
var myRequest = new XMLHttpRequest();
// 2. usa il metodo open per richiedere una risorsa sul sito remoto
myRequest.open('GET', 'http://www.classiperlo.altervista.org/File%20comuni/ora_ajax.php?y=' + Math.random(),true);
// 3. ogni volta che cambia lo stato dell'oggetto esegue una funzione
myRequest.onreadystatechange = function () { 
    // 4. readyState === 4 indica che il server ha risposto
    if (myRequest.readyState === 4) {
        // 5. inserisce il contenuto della risposta in un DIV
        document.getElementById('ajax-content').innerHTML = myRequest.responseText;
    }
};

function sendTheAJAX() {
    myRequest.send();
}

I don't understand why, when I click again, I receive always the same information from the server. 我不明白为什么,当我再次单击时,总是会收到来自服务器的相同信息。 I mean, my page ora_ajax.php simply displays current time in php and this information is not refreshed when I send a new request. 我的意思是,我的页面ora_ajax.php仅显示php中的当前时间,并且在我发送新请求时不会刷新此信息。

Where is my error? 我的错误在哪里? Thanks a lot for helping! 非常感谢您的帮助!

Try to put your 'request creation and open' code in another function and call that function when you want to send another request. 尝试将“请求创建和打开”代码放入另一个函数中,并在要发送另一个请求时调用该函数。

function sendRequest(){
  var myRequest = new XMLHttpRequest();
  ...
return myRequest;
}

function sendTheAJAX() { sendRequest(); }

Thanks for your kind help. 感谢您的帮助。 Solved simply putting all the code inside the function triggered by onclick event: 只需将所有代码放入onclick事件触发的函数中即可解决:

function sendTheAJAX() {
// 1. crea un oggetto di tipo XMLHttpRequest
var myRequest = new XMLHttpRequest();
// 2. usa il metodo open per richiedere una risorsa sul sito remoto
myRequest.open('GET', 'http://www.classiperlo.altervista.org/File%20comuni/ora_ajax.php?y=' + Math.random(),true);
// 3. ogni volta che cambia lo stato dell'oggetto esegue una funzione
myRequest.onreadystatechange = function () { 
    // 4. readyState === 4 indica che il server ha risposto
    if (myRequest.readyState === 4) {
        // 5. inserisce il contenuto della risposta in un DIV
        document.getElementById('ajax-content').innerHTML = myRequest.responseText;
    }
};

    myRequest.send();
}

In this way a new object myRequest is created at every click on the button. 这样,每次单击按钮都会创建一个新对象myRequest。

Put all of the XHR code inside sendTheAJAX above .send() , then call the entire thing, otherwise the Math.random() never changes. 将所有XHR代码放在sendTheAJAX .send()上方的.send()内,然后调用整个内容,否则Math.random()永不更改。

After reading your comment, you can just remove that part. 阅读评论后,您可以删除该部分。 Seems you don't need it. 似乎您不需要它。

Note that if you were to use it Math.random() returns a number between 0 and 0.99999999, never 1. 请注意,如果要使用它, Math.random()将返回一个介于0到0.99999999之间的数字,从不返回1。

function sendTheAJAX(successFunc, context){
  var xhr = new XMLHttpRequest, c = context || this;
  xhr.open('GET', 'http://www.classiperlo.altervista.org/File%20comuni/ora_ajax.php');
  if(xhr.readyState === 4 && xhr.status === 200){
    successFunc.call(c, xhr.responseText);
  }
  xhr.send();
  return xhr;
}
function yup(){
  sendTheAJAX(function(respTxt){
    document.getElementById('ajax-content').innerHTML = respTxt;
  });
}
yup(); // remove if you don't want call here
document.body.onclick = yup;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM