简体   繁体   中英

how to abort a xmlhttprequest before starting a new request?

I got a cgi-script the performs a search according to the query-string it gets submitted. That works all fine. But since the search can take some time and the user might start another search. I want the first search to get aborted before a second search is started, but if I insert a xmlhttp.abort() at the beginning of my AJAXRequest function the cgi-script doesn't get started at all. Can anyone tell me how this could be performed?

Thanks in advance!

and here is the AJAX-code snippet

var xmlhttp;
function AJAXRequest()
{

   if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
   window.document.getElementById("resultlist").innerHTML=xmlhttp.responseText;
}
}
var queryString= "from=03-01-2006&to=04-30-2006&pattern=345";
xmlhttp.open("POST","../cgi-bin/search.exe?"+queryString);
xmlhttp.send();
}

Yea, XMLHttpRequest.abort() is a correct method to use for AJAX request aborting. In my opinion your problem is more kind of design problem and not technical. Therefore my suggestion is to review the flow of how the user interacts with your application. For example, the classical solution is to show the progress bar or spinner and disable the "Search" button while your request is not completed and revert this state on success or error.

I found my problem. I tried to abort a request object that has just been created. The object has to be global. At least that's the only way I see how to achieve this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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