我无法使用 jQuery 和 abort() 函数停止 ajax 请求

发布于 2024-10-02 18:52:10 字数 533 浏览 0 评论 0原文

我的 jQuery 如下:

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()​​

您可以在这里尝试此代码: http://jsbin.com/iyile3/2/edit

我想要停止这个ajax请求并停止这个ajax请求的成功函数。

但我得到的是“x”未定义,我想我不会停止这个ajax请求及其成功函数。

那么有人可以帮助我吗?

非常感谢!

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!

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

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

发布评论

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

评论(2

飘落散花 2024-10-09 18:52:10

嗯,这是有道理的。

jsonp 调用不是普通的 XMLHttpRequest(.ajax() 通常返回)。这种方式是行不通的,因为您无法突破同源策略

JSON-Padding 适用于动态脚本标签插入。所以基本上,jQuery 只是在您的网站上创建一个

我不认为有办法提前中止这种请求,但我希望如果我错了,有人会纠正我。

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.

一笔一画续写前缘 2024-10-09 18:52:10

我认为 文档 在这方面可能有点误导。使用 JSONP 时不会发出 AJAX 请求。在这种情况下,ajax 函数返回一个undefined 值,而不是XMLHTTPRequest 的对象。

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.

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