can somehow change the callback function name?
Solution 1
You are close. This works perfectly:
function getPhoto() {
$.ajax({
url: "http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&format=json&api_key=fbfe07eb3cc28814df5bbc0313cdd521",
dataType: "jsonp",
jsonp: 'jsoncallback',
success: function(data) {
alert(data);
}
});
}
getPhoto();
As the documentation describes, you can set your own callback name with the jsoncallback
parameter. Hence we have to set jsonp: 'jsoncallback'
. In the jQuery documentation you can find that it is recommended to let jQuery choose a callback name. Just set the success
callback and you are done.
Solution 2
From the Flickr API docs:
If you just want the raw JSON, with no function wrapper, add the parameter nojsoncallback with a value of 1 to your request.
To define your own callback function name, add the parameter jsoncallback with your desired name as the value.
nojsoncallback=1 -> {...}
jsoncallback=wooYay -> wooYay({...});
Example:
http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&format=json&api_key=fbfe07eb3cc28814df5bbc0313cdd521&jsoncallback=myCallbackFun
Returns:
myCallbackFun({"photos":{"page":1, "pages":5, "perpage":100, "total":500, "photo":[{"id":"5623656271", "owner":"50725098@N08", "secret":"b67514798d", "server":"5143", "farm":6, "title":"Defying Gravity!!!", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"5624056667", "owner":"51832166@N03", "secret":"57ffca018d", "server":"5301", "farm":6, "title":"Navy Officers: Pearl Harbor", "i...
Clinteney Hui
Updated on June 09, 2022Comments
-
Clinteney Hui almost 2 years
Hey, I am doing to AJAX call to "flickr.interestingness.getList" to get the interesting pictures and this is my AJAX call.
function getPhoto() { $.ajax("http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&format=json&api_key=fbfe07eb3cc28814df5bbc0313cdd521", { dataType: "jsonp", //jsonp: false, jsonFlickrApi: "jsonpcallback", jsonpCallback: "jsonFlickrApi", }); } function jsonFlickrApi(data) { alert(data.photos.photo); }
and here "JsonFlickrApi" is the pre-defined function from Flickr that wraps the json object which has a bunch of photos. My question is could I somehow override the pre-defined function, "jsonFlickApi" and name the callback function something other than "jsonFlickrApi", I thought the jsonp parameter is supposed to do that after I read the jQuery documentation but just failed to change it.or I dont quite understand what the jsonp parameter does in jQuery AJAX call. thank you
-
Clinteney Hui about 13 yearsyeah, I guess what I dont understand is that since I can set my own call back name, I am supposed to name it whatever I want. right? I've tried do it like this jsonp: 'jsonpcallback', but it throws an error saying jsonFlickrApi is not defined. how so? Thank you
-
Felix Kling about 13 years@ClinteneyHui: I don't know what else you are doing. The code above works. Yes, you can name it whatever you want but you don't have to. Why not let jQuery handle it?
-
Clinteney Hui about 13 yearsI got it. misunderstood a little bit. Thanx
-
SubmittedDenied almost 9 yearsSomething to note here. Setting
jsonp
in the ajax object changes the key of the query string parameter for jsonp. SettingjsonpCallback
sets the value of that key. For example:{jsonp: 'foo', jsonpCallback: 'bar'}
would result in the url looking like:...?foo=bar...