为什么 jQuery 不自动附加 JSONP 回调?

发布于 2024-08-24 17:39:00 字数 1449 浏览 2 评论 0原文

$.getJSON() 文档指出:

如果指定的 URL 位于远程服务器上,则该请求将被视为 JSONP。有关更多详细信息,请参阅 $.ajax() 中对 jsonp 数据类型的讨论。

$.ajax() 文档jsonp 数据类型状态(强调我的):

使用 JSONP 加载 JSON 块。 会添加额外的“?callback=?”添加到 URL 末尾以指定回调

因此,如果我使用跨域 URL 调用 $.getJSON() ,则会出现额外的“callback=?”参数应该自动添加。 (文档的其他部分支持这种解释。)

但是,我没有看到这种行为。如果我不添加“callback=?”明确地,jQuery 错误地生成了 XMLHttpRequest(它返回空数据,因为我无法跨域读取响应)。如果我明确添加它,jQuery 会正确地生成一个

这是一个例子:

var URL = "http://www.geonames.org/postalCodeLookupJSON" +
    "?postalcode=10504&country=US";

function alertResponse(data, status) {
  alert("data: " + data + ", status: " + status);
}

$.getJSON(URL, alertResponse);
// alerts "data: null, status: success"

$.getJSON(URL + "&callback=?", alertResponse);
// alerts "data: [object Object], status: undefined"

那么发生了什么?我是否误解了文档或忘记了什么?

不用说,这不是什么大问题,但我正在创建一个 Web API,并且我特意将回调参数设置为“callback”,希望能够很好地适应 jQuery 的使用。

谢谢!

(编辑:我 如果您有兴趣,请在 jQuery 论坛中交叉发布。)

The $.getJSON() documentation states:

If the specified URL is on a remote server, the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.

The $.ajax() documentation for the jsonp data type states (emphasis mine):

Loads in a JSON block using JSONP. Will add an extra "?callback=?" to the end of your URL to specify the callback.

So it seems that if I call $.getJSON() with a cross-domain URL, the extra "callback=?" parameter should automatically get added. (Other parts of the documentation support this interpretation.)

However, I'm not seeing that behavior. If I don't add the "callback=?" explicitly, jQuery incorrectly makes an XMLHttpRequest (which returns null data since I can't read the response cross-domain). If I do add it explicitly, jQuery correctly makes a <script> request.

Here's an example:

var URL = "http://www.geonames.org/postalCodeLookupJSON" +
    "?postalcode=10504&country=US";

function alertResponse(data, status) {
  alert("data: " + data + ", status: " + status);
}

$.getJSON(URL, alertResponse);
// alerts "data: null, status: success"

$.getJSON(URL + "&callback=?", alertResponse);
// alerts "data: [object Object], status: undefined"

So what's going on? Am I misunderstanding the documentation or forgetting something?

It goes without saying that this isn't a huge deal, but I'm creating a web API and I purposely set the callback parameter to "callback" in the hopes of tailoring it nicely to jQuery usage.

Thanks!

(Edit: I cross-posted this in the jQuery forums if you're interested.)

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

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

发布评论

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

评论(4

倾城泪 2024-08-31 17:39:00

事实证明这是 jQuery 文档中的一个错误。请参阅 http:// forum.jquery.com/topic/getjson-isn-t-automatically-appending-callback-to-my-cross-domain-url 了解详细信息。

Turns out this was a bug in the jQuery documentation. See http://forum.jquery.com/topic/getjson-isn-t-automatically-appending-callback-to-my-cross-domain-url for details.

离旧人 2024-08-31 17:39:00

试试这个:

var URL = "http://www.geonames.org/postalCodeLookupJSON" +
    "?postalcode=10504&country=US";
function alertResponse(data, status) {
    alert("data: " + data + ", status: " + status);
}
$.ajax({
    url: URL,
    dataType: 'jsonp',
    jsonpCallback: 'alertResponse',
});

Try this:

var URL = "http://www.geonames.org/postalCodeLookupJSON" +
    "?postalcode=10504&country=US";
function alertResponse(data, status) {
    alert("data: " + data + ", status: " + status);
}
$.ajax({
    url: URL,
    dataType: 'jsonp',
    jsonpCallback: 'alertResponse',
});
给妤﹃绝世温柔 2024-08-31 17:39:00

是的,我认为你误解了。 $.getJSON$.ajax({datatype: 'json'.... 如文档所述。它永远不会进行 JSONP 调用,除非您添加callback=? 参数。

Yes, I think you misunderstood. $.getJSON is a shortcut for $.ajax({datatype: 'json'.... as the documentation says. It never makes a JSONP call unless you add the callback=? parameter.

埖埖迣鎅 2024-08-31 17:39:00

我正在使用下面的代码,

$.ajax({
网址:网址,
数据类型:'jsonp',
成功:函数(数据)
{
// 做某事
}
错误:函数(jqXHR,textStatus,errorThrown){},
jsonpCallback: '登录回调',
});

但是,回调有时会附加在 url 末尾,有时不会在 IE 中附加。
虽然它在 chrome 和 FF 中运行良好。

I am using below code,

$.ajax({
url: URL,
dataType: 'jsonp',
success: function ( data )
{
// do something
}
error: function (jqXHR, textStatus, errorThrown) { },
jsonpCallback: 'login_callback',
});

But, callback sometimes gets appended at the end of the url and sometimes not in IE.
While its working fine in chrome and FF.

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