问题
My jQuery is below:
var x = $.ajax({
dataType: "jsonp",
url: "https://ajax.googleapis.com/ajax/services/search/images?q=google&v=1.0",
success: function(msg){
alert( "Jsonp data: " + msg );
}
});
alert(x); // x is undefined
// x.abort()
You can try this code here: http://jsbin.com/iyile3/2/edit
I want stop this ajax request and stop this ajax request's success function.
But what I get is that "x" is undefined , I think I doesn't stop this ajax request and its success function.
So can anyone help me?
Thank you very much!
回答1:
Well that makes sense.
A jsonp
call is not an ordinary XMLHttpRequest
(which .ajax() normally returns). It would not work that way since you cannot break out the same origin policy
.
JSON-Padding works with dynamic script tag insertion. So basically, jQuery just creates a <script>
tag on your site and loads the data over that. No XHR
object at all.
I don't think there is a way to early abort that kind of request, but I hope someone will correct me here if I'm wrong.
回答2:
I think the documentation may be a little misleading on this one. No AJAX request is made when using JSONP. In this case, the ajax
function returns an undefined
value instead of the object of XMLHTTPRequest
.
来源:https://stackoverflow.com/questions/4162261/i-cannot-stop-an-ajax-request-using-jquery-and-abort-function