在使用jQuery请求JSONP时,我可以使用静态(即预定的)回调函数名称吗?

时间:2022-12-08 19:43:26

The jQuery documentation lists the following example of using $.getJSON to request JSONP:

jQuery文档列出了以下使用$ .getJSON请求JSONP的示例:

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?",
  function(data) {
    $.each(data.items, function(i,item) {
      $("<img/>").attr("src", item.media.m).appendTo("#images");
      if (i == 3) return false;
    });
  });

Rather than use this method, which generates a dynamic callback function name because of this parameter:

而不是使用此方法,因为此参数生成动态回调函数名称:

jsoncallback=?

I want to be able to set that in advance to a hardcoded function name, like this:

我希望能够事先将其设置为硬编码的函数名称,如下所示:

jsoncallback=test

This works, in the sense that I run the script and the JSONP that I get back has the JSON object wrapped in a call to test().

这是有效的,在我运行脚本的意义上,我得到的JSONP将JSON对象包含在对test()的调用中。

However, I can't figure out how to set up the callback function. Shouldn't it be as simple as this?

但是,我无法弄清楚如何设置回调函数。它不应该这么简单吗?

function test(data) {
  console.log(data);
}

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=test");

When I try that, I get back the JSONP which is wrapped in test(), but the function test() that I've defined is never called. Am I missing something?

当我尝试这个时,我得到了包含在test()中的JSONP,但是我定义的函数test()从未被调用过。我错过了什么吗?

Thanks for any help!

谢谢你的帮助!

2 个解决方案

#1


3  

As defined in the documentation for you to use the following method

如文档中所定义,您可以使用以下方法

jQuery.getJSON(...)

you need to specify callback=? when making a JSONP call. I usually only uses this for response types of "json". For response types of "jsonp", you want to use:

你需要指定callback =?在进行JSONP调用时。我通常只将它用于“json”的响应类型。对于“jsonp”的响应类型,您要使用:

jQuery.get(...)

and specify the type as "jsonp". See this documentation on the subject. But that is also bound by the fact of having to have a callback=?.

并将类型指定为“jsonp”。请参阅有关该主题的此文档。但这也受到必须要回调=?的限制。

What I think you are looking for is this:

我认为你在寻找的是:

jQuery.getScript(...)

Which should execute whatever method you have defined in your callback.

哪个应该执行你在回调中定义的任何方法。

#2


1  

Ah, the "Related" sidebar section saved me here. After I submitted this question, I found a similar one already asked:

啊,“相关”侧栏栏节省了我这里。在我提交这个问题之后,我发现了一个类似的问题:

using a named function as the callback for $.getJSON in jQuery to satisfy Facebook request signing demands

使用命名函数作为jQuery中$ .getJSON的回调来满足Facebook请求签名要求

Duncan's answer from Oct. 15 solved this for me:

Duncan从10月15日开始的回答为我解决了这个问题:

window.fixed_callback = function(data){
  alert(data.title);
};

$(function() {
  $.getScript("http://api.flickr.com/services/feeds/photos_public.gne?tags=cats&tagmode=any&format=json&jsoncallback=fixed_callback", function(data) {
  alert('done'); } );
});

I guess the key is using $.getScript instead of $.getJSON. One can still specify an anonymous callback function in the parameters of the $.getScript method, which will be executed after the callback function named in the request URL parameters ("fixed_callback" in this case). Hope this helps someone down the road.

我想关键是使用$ .getScript而不是$ .getJSON。仍然可以在$ .getScript方法的参数中指定匿名回调函数,该函数将在请求URL参数中指定的回调函数(在本例中为“fixed_callback”)之后执行。希望这可以帮助有人在路上。

#1


3  

As defined in the documentation for you to use the following method

如文档中所定义,您可以使用以下方法

jQuery.getJSON(...)

you need to specify callback=? when making a JSONP call. I usually only uses this for response types of "json". For response types of "jsonp", you want to use:

你需要指定callback =?在进行JSONP调用时。我通常只将它用于“json”的响应类型。对于“jsonp”的响应类型,您要使用:

jQuery.get(...)

and specify the type as "jsonp". See this documentation on the subject. But that is also bound by the fact of having to have a callback=?.

并将类型指定为“jsonp”。请参阅有关该主题的此文档。但这也受到必须要回调=?的限制。

What I think you are looking for is this:

我认为你在寻找的是:

jQuery.getScript(...)

Which should execute whatever method you have defined in your callback.

哪个应该执行你在回调中定义的任何方法。

#2


1  

Ah, the "Related" sidebar section saved me here. After I submitted this question, I found a similar one already asked:

啊,“相关”侧栏栏节省了我这里。在我提交这个问题之后,我发现了一个类似的问题:

using a named function as the callback for $.getJSON in jQuery to satisfy Facebook request signing demands

使用命名函数作为jQuery中$ .getJSON的回调来满足Facebook请求签名要求

Duncan's answer from Oct. 15 solved this for me:

Duncan从10月15日开始的回答为我解决了这个问题:

window.fixed_callback = function(data){
  alert(data.title);
};

$(function() {
  $.getScript("http://api.flickr.com/services/feeds/photos_public.gne?tags=cats&tagmode=any&format=json&jsoncallback=fixed_callback", function(data) {
  alert('done'); } );
});

I guess the key is using $.getScript instead of $.getJSON. One can still specify an anonymous callback function in the parameters of the $.getScript method, which will be executed after the callback function named in the request URL parameters ("fixed_callback" in this case). Hope this helps someone down the road.

我想关键是使用$ .getScript而不是$ .getJSON。仍然可以在$ .getScript方法的参数中指定匿名回调函数,该函数将在请求URL参数中指定的回调函数(在本例中为“fixed_callback”)之后执行。希望这可以帮助有人在路上。