Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can somehow change the callback function name?

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

like image 985
Clinteney Hui Avatar asked Dec 08 '25 23:12

Clinteney Hui


1 Answers

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();

DEMO

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.

like image 200
Felix Kling Avatar answered Dec 11 '25 13:12

Felix Kling