使用jQuery中止JSONP ajax请求 [英] Abort JSONP ajax request with jQuery
问题描述
我使用JSONP ajax调用从不同的域加载一些内容,如果用户在按钮上造成鼠标悬停,所有这些内容都会执行。
I'm using a JSONP ajax call to load some content from a different domain, and all this stuff is executed if the user causes a "mouseover" on a button.
我可以捕获$ .ajax()调用返回作为一个xhr对象,并使用它来中止ajax请求每次用户导致鼠标悬停。但JSONP回调函数仍然被调用,这导致一个错误,我认为这是beacuse xhr.abort()方法不会阻止回调函数被调用。
I can capture the $.ajax() call return as a xhr object, and use it to abort the ajax request each time the user causes a "mouseover". But the JSONP callback function still gets called, and this causes an error, and I think it is beacuse the xhr.abort() method does not prevent the callback function to be called.
我尝试用try {} catch(e){}调用$ .ajax()调用,但在调用xhr.abort()方法后,错误继续。
I've tried surrounding the $.ajax() call with try{}catch(e){}, but after I call the xhr.abort() method, the error continues.
有办法处理这个异常吗?
Is there a way to handle that exception?
引发异常是这样的(根据Firebug):
jQuery16102234208755205157_1308941669256不是函数
The raised exception is like this (according to Firebug): jQuery16102234208755205157_1308941669256 is not a function
异常的内部结构如下:
jQuery16102234208755205157_1308941669256({... my json data from不同的域....})
And the exception's internal structure is like this: jQuery16102234208755205157_1308941669256({... my json data from a different domain....})
推荐答案
在jQuery 1.5中,所有的Ajax API都有一个包装器对象原生XHR对象。请查看:
In jQuery 1.5 all the Ajax APIs have a wrapper object around the native XHR objects. Take a look at:
http: //api.jquery.com/jQuery.ajax/#jqXHR
jqxhr.abort(); // should be what you're looking for
这篇关于使用jQuery中止JSONP ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!