如何使用javascript取消http请求

发布于 2024-11-27 03:28:29 字数 895 浏览 1 评论 0原文

我有一个页面,其中有一个事件处理程序附加到 onclick 事件。当事件触发时,它将文本框的内容传递给 GET 请求。因为网址不在同一个域,所以我创建一个脚本标记,并将网址附加到其源,就像

elem.onclick=fire;

function fire()
{
var text=document.getElementById('text').value;
var script=document.createElement("script");
script.className="temp";
script.src="some url"+"?param="+text;
document.body.appendChild(script);
}

现在这样,如果该事件被触发并且不止一次我想取消所有先前的事件GET 请求(因为它们仍然可能收到响应)并使用最新文本发出 GET 请求。但为此我需要取消之前的请求。 我尝试了

document.body.removeChild(script);
script.src=null;

,但这在Firefox中不起作用(我使用的是Firefox 5),尽管这在Google Chrome中有效.有谁知道这些请求是否可以在 Firefox 中取消,如果可以,那么如何取消?


更新 按照 Alfred 的建议,我使用 window.stop 来取消请求,但不会取消请求,而是将其挂起。这意味着当我查看 firebug 时,看起来好像正在发出请求,但没有响应。

i have a page on which there an event handler attached to an onclick event. when the event fires it passes contents of a textbox to a GET request. since the url is not in the same domain so i create a script tag and and attach the url to its source like this

elem.onclick=fire;

function fire()
{
var text=document.getElementById('text').value;
var script=document.createElement("script");
script.className="temp";
script.src="some url"+"?param="+text;
document.body.appendChild(script);
}

now if that event is fired and more than one time i want to cancel all the previous GET request(because they still might be receiving response) and make the GET request with latest text. But for this i need to cancel the previous requests.
i tried

document.body.removeChild(script);
script.src=null;

but this does not work in Firefox(i am using Firefox 5) although this works in Google Chrome.Does anyone know if these requests can be cancelled in Firefox and if yes then how?


UPDATE
As suggested by Alfred, i used window.stop to cancel a request but does not cancel a request but hangs it up. It means that when i look into firebug it looks like the request is being made but there is no response.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝色星空 2024-12-04 03:28:29

解决方案很简单:要创建 HTTP 请求,请使用 而不是

var img;
function fire()
{
    var text = document.getElementById('text').value;
    var im = img || (img = new Image());
    im.src = "url"+"?param="+text;
}

您可以通过执行以下操作来确定它实际上是否有效:您请求的 URL 应该有较长的响应时间(您可以使用 PHP 的 sleep 函数来确保这一点)。然后,在 Firebug 中打开“网络”选项卡。如果您多次单击该按钮,您将看到所有不完整的请求都将被中止。

The solution is simple: for creating HTTP requests, use <img> instead of <script> element. Also you always have to change the src attribute of the same element.

var img;
function fire()
{
    var text = document.getElementById('text').value;
    var im = img || (img = new Image());
    im.src = "url"+"?param="+text;
}

You may ascertain that it actually works by doing the following: the URL you request should have a huge response time (you can ensure this using e.g. PHP's sleep function). Then, open Net tab in Firebug. If you click the button multiple times, you'll see that all incomplete requests are aborted.

夏日落 2024-12-04 03:28:29

这完全是凭空而来,但如果 script 标签尚未完成加载,您可能可以简单地 script.parentElement.removeChild( script ) 。这或多或少就是 mootools 所做的事情。 (从技术上讲,他们首先将 /\s+/ 替换为 ' ',但这似乎并不是非常重要)。

This is entirely shooting from the hip, but if the script tag has not finished loading you can probably simply script.parentElement.removeChild( script ). That is more or less what mootools does anyway. (Technically, they replace /\s+/ with ' ' first, but that does not seem to be terribly important).

留蓝 2024-12-04 03:28:29

你用JS框架可以吗?如果是这样,MooTools 已将此功能内置到其 Request.JSONP 对象中

Would it be ok for you to use a JS framework? If so, MooTools has this functionality built into its Request.JSONP object

不及他 2024-12-04 03:28:29

我不确定这是否是您正在寻找的问题,但这似乎是一个类似的问题:
http://www.velocityreviews.com /forums/t506018-如何取消-http-request-from-javascript.html

I'm not sure if this is what you're looking for, but it seems like a similar issue:
http://www.velocityreviews.com/forums/t506018-how-to-cancel-http-request-from-javascript.html

宫墨修音 2024-12-04 03:28:29

要解决跨域问题,您也许可以使用 CORS(假设您可以更改服务器上的内容):
http://hacks.mozilla.org/2009/07/ cross-site-xmlhttprequest-with-cors/

如果您这样做,则可以使用更标准的 XMLHttpRequest 的 abort() 函数。

CORS 与除 Opera (http://caniuse.com/cors) 之外的所有主要现代浏览器兼容。

To get around the cross-domain issue, you might be able to use CORS instead (assuming you can change what's on the server):
http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/

If you do this, you could then use the more standard XMLHttpRequest's abort() function.

CORS is compatible with all the major modern browsers except Opera (http://caniuse.com/cors).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文