如何使用javascript取消http请求
我有一个页面,其中有一个事件处理程序附加到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
解决方案很简单:要创建 HTTP 请求,请使用
而不是
元素。此外,您始终必须更改 same 元素的
src
属性。您可以通过执行以下操作来确定它实际上是否有效:您请求的 URL 应该有较长的响应时间(您可以使用 PHP 的
sleep
函数来确保这一点)。然后,在 Firebug 中打开“网络”选项卡。如果您多次单击该按钮,您将看到所有不完整的请求都将被中止。The solution is simple: for creating HTTP requests, use
<img>
instead of<script>
element. Also you always have to change thesrc
attribute of the same element.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.这完全是凭空而来,但如果 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).你用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
我不确定这是否是您正在寻找的问题,但这似乎是一个类似的问题:
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
要解决跨域问题,您也许可以使用 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).