Search code examples
jqueryjsonpjquery-callback

Prevent jquery from appending its own callback


I'm trying to call Vimeo's API using $.ajax(), but Jquery appends a callback to my URL even when I specify my own named function. I need complete control over the GET URL string.

My code:

function fback(data) {
    alert('data = ' + data);
}

$.ajax({
    url: 'http://vimeo.com/api/v2/group/processing/videos.json?format=jsonp&callback=fback',
    dataType: "jsonp",
    type: "GET",
    cache: true,
    success: fback,
});

The GET request goes to:

http://vimeo.com/api/v2/group/processing/videos.json?format=jsonp&callback=fback&callback=jsonp1291384300228

How can I avoid this automatic callback addition?


Solution

  • You just need to specify the jsonpCallback option to be the function name you're after, like this:

    $.ajax({
        url: 'http://vimeo.com/api/v2/group/processing/videos.json?format=jsonp&callback=?',
        jsonpCallback: "fback",
        dataType: "jsonp",
        type: "GET",
        cache: true
    });
    

    What this does is instead of that randomly generated name (well not so random, but you get the point), it'll use `"fback", resulting in:

    http://vimeo.com/api/v2/group/processing/videos.json?format=jsonp&callback=fback
    

    You can test it out here.