简体   繁体   English

如何在开始新请求之前中止xmlhttprequest?

[英]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. 我得到了一个cgi脚本,它根据提交的查询字符串执行搜索。 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. 我希望第一个搜索在开始第二个搜索之前被中止,但是如果我在AJAXRequest函数的开头插入xmlhttp.abort() ,则cgi-script根本不会启动。 Can anyone tell me how this could be performed? 谁能告诉我该如何执行?

Thanks in advance! 提前致谢!

and here is the AJAX-code snippet 这是AJAX代码段

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. 是的, XMLHttpRequest.abort()是用于AJAX请求中止的正确方法。 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. 至少这是我看到如何实现这一目标的唯一方法。

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

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