jQuery为什么不自动附加JSONP回调? [英] Why isn't jQuery automatically appending the JSONP callback?
问题描述
$.getJSON()
文档指出:
The $.getJSON()
documentation states:
如果指定的URL在远程服务器上,则该请求将被视为JSONP.有关更多详细信息,请参见$ .ajax()中有关
jsonp
数据类型的讨论.
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.
jsonp
数据类型状态的 $.ajax()
文档(重点是):
The $.ajax()
documentation for the jsonp
data type states (emphasis mine):
使用JSONP加载到JSON块中. 将添加额外的?callback =?"到您的网址末尾以指定回调.
因此,如果我使用跨域URL调用$.getJSON()
,则似乎会出现额外的"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.)
但是,我没有看到这种行为.如果我不添加"callback =?"显式地,jQuery错误地生成了XMLHttpRequest(由于我无法读取响应的跨域,因此它返回空数据).如果我确实明确添加了它,则jQuery会正确地创建一个< script>请求.
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.
这是一个例子:
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?
毋庸置疑,这并不是什么大问题,但是我正在创建一个Web API,我特意将callback参数设置为"callback",以期更好地适应jQuery的使用.
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.
谢谢!
(我推荐答案
原来这是jQuery文档中的错误.参见 http://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. 这篇关于jQuery为什么不自动附加JSONP回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!