Can I use the static (i.e. Predefined) name of the callback function when requesting JSONP using jQuery?

The jQuery documentation contains the following example of using $ .getJSON to request a 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;
    });
  });

Instead of using this method, which generates a dynamic callback function name because of this parameter:

jsoncallback=?

I want to be able to pre-set this name for hard coding, for example:

jsoncallback=test

This works in the sense that I am running a script, and the JSONP that I have has a JSON object wrapped in a test () call.

However, I cannot figure out how to configure the callback function. Isn't it that simple?

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 to do this, I return a JSONP that is wrapped in test (), but the test () function that I defined is never called. Did I miss something?

!

+3
2

,

jQuery.getJSON(...)

callback=? JSONP. "json". "jsonp" :

jQuery.get(...)

type "jsonp". . a callback=?.

, :

jQuery.getScript(...)

, callback.

+3

, "" . , , , :

$.getJSON jQuery Facebook

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'); } );
});

, $.getScript $.getJSON. $.getScript, , URL- ( - "fixed_callback" ). , - .

+1

Source: https://habr.com/ru/post/1720803/


All Articles