开发者

Prevent jquery from appending its own callback

开发者 https://www.devze.com 2023-01-28 21:54 出处:网络
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.

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('开发者_JS百科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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号