HOW TO handle a API's JSONP response that uses a static or fixed or named callback function


Script and JSONP requests are not subject to the same origin policy restrictions. Most APIs that return JSONP data will generate a dynamic callback function name and they typically have a parameter like jsoncallback=?.

However some API's like flickr, Facebook require that you use the callback function name that they specify.

Here are a couple of ways that you can tackle that scenario -

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'); } );
});
click to enlarge code snippet

Comments

Popular posts from this blog

Maven Crash Course - Learn Power Query, Power Pivot & DAX in 15 Minutes

"Data Prep & Exploratory Data Analysis" course by Maven Analytics

Oracle Cloud Infrastructure 2024 Generative AI Professional Course & Certification Exam (1Z0-1127-24)