可以以某种方式更改回调函数名称吗?

发布于 2024-11-01 13:53:14 字数 690 浏览 0 评论 0原文

嘿,我正在对“flickr.interestingness.getList”进行 AJAX 调用以获取有趣的图片,这是我的 AJAX 调用。

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

这里的“JsonFlickrApi”是 Flickr 的预定义函数,它包装了包含一堆照片的 json 对象。我的问题是我是否可以以某种方式覆盖预定义函数“jsonFlickApi”并将回调函数命名为“jsonFlickrApi”以外的其他名称,我认为 jsonp 参数应该在我阅读 jQuery 文档后执行此操作,但未能更改它或者我不太明白 jsonp 参数在 jQuery AJAX 调用中的作用。谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

讽刺将军 2024-11-08 13:53:14

你很接近。这完美地工作:

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

作为 文档描述,您可以通过jsoncallback参数设置自己的回调名称。因此我们必须设置 jsonp: 'jsoncallback' 。在jQuery文档中你可以发现建议让jQuery选择一个回调名称。只需设置 success 回调即可完成。

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.

暗恋未遂 2024-11-08 13:53:14

来自 Flickr API 文档

如果你只想要原始 JSON,没有
函数包装器,添加参数
nojsoncallback 值为 1
您的要求。

定义自己的回调函数
name,添加参数jsoncallback
将您想要的名称作为值。

nojsoncallback=1    -> {...}
jsoncallback=wooYay -> wooYay({...});

示例:

http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&format=json&api_key=fbfe07eb3cc28814df5bbc0313cdd521&jsoncallback=myCallbackFun

返回:

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...

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...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文