如何更改mootools类Request.JSONP中的回调函数

发布于 2024-12-03 08:26:28 字数 876 浏览 3 评论 0原文

我正在使用 Request.JSONP 来获取一些数据,但我需要更改检索数据的 url 中的回调函数。

使用文档中的示例:

如何更改此调用:

http ://www.flickr.com/?format=json&jsoncallback=Request.JSONP.request_map.request_0

到此:

http://www.flickr.com/?format=json&jsoncallback=myOwnFunction

在 jQuery 中,您可以使用设置“jsonpCallback”来使用您自己的函数,但我找不到在 MooTools 中更改它的方法。

任何帮助、想法,将不胜感激。

编辑:

我的问题是我的函数将位于 setInterval 函数中,并且每次执行请求的 url 更改时,如下所示:

callback=Request.JSONP.request_map.request_0callback

=Request.JSONP。 request_map.request_1

callback=Request.JSONP.request_map.request_2...n

我需要最后一位数字不改变(出于缓存目的)

I'm using the Request.JSONP to get some data, but I need to change the callback function in the url that retrieve de data.

Using the example in the documentation:

How to change this call:

http://www.flickr.com/?format=json&jsoncallback=Request.JSONP.request_map.request_0

to this:

http://www.flickr.com/?format=json&jsoncallback=myOwnFunction

In jQuery you can use the setting "jsonpCallback" to use your own function, but I can't find a way to change it in MooTools.

Any help, ideas, will be greatly appreciated.

Edit:

My problem is that my function is going to be in a setInterval function and every time it executes the requested url change like this:

callback=Request.JSONP.request_map.request_0

callback=Request.JSONP.request_map.request_1

callback=Request.JSONP.request_map.request_2...n

I need that the last digit doesn't change (for cache purposes)

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

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

发布评论

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

评论(1

自控 2024-12-10 08:26:28

对于 JSONP,它称为 callbackKey阅读文档

这是一个扩展我编写的 jsonp 的 flickr 类(请更改 api):

// the class
Request.flickr = new Class({
    Extends: Request.JSONP,
    options: {
        callbackKey: "jsoncallback",
        url: "http://www.flickr.com/services/rest/?",
        log: true
    },
    initialize: function(params, options) {
        this.parent(options);
        this.options.url = this.options.url + Object.toQueryString(params);
    },
    success: function(data, script) {
        this.parent(data, script);
    },
    imageURL: function(obj) {
        return "http://farm{farm}.static.flickr.com/{server}/{id}_{secret}.jpg".substitute(obj);
    }
});

// example on how to use
new Request.flickr({
    format: 'json',
    api_key: "e7df6c74d2545f55414423463bf99723", // your api here
    per_page: 4,
    tags: "mountains",
    method: "flickr.photos.search"
}, {
    onSuccess: function(data) {
        target = document.id("action");
        var self = this;
        data.photos.photo.each(function(el) {
            new Asset.image(self.imageURL(el), {
                onload: function() {
                    this.inject(target);
                }
           });
        });
    }
}).send();

要完全覆盖回调并传递外部函数(需要是全局的):

window.foo = function(data) {
    console.log(data);
};


Request.flickr = new Class({
    Extends: Request.JSONP,
    options: {       
        url: "http://www.flickr.com/services/rest/?jsoncallback=foo&",
        log: true
    },
    initialize: function(params, options) {
        this.parent(options);
        this.options.url = this.options.url + Object.toQueryString(params);
    },
    success: function(data, script) {
        this.parent(data, script);
    },
    imageURL: function(obj) {
        return "http://farm{farm}.static.flickr.com/{server}/{id}_{secret}.jpg".substitute(obj);
    }
});

// example on how to use
new Request.flickr({
    format: 'json',
    api_key: "e7df6c74d2545f55414423463bf99723",
    // your api here
    per_page: 4,
    tags: "mountains",
    method: "flickr.photos.search"
}).send();

上面的代码将运行函数 foo,并将 JSON 对象传递给它。 http://jsfiddle.net/dimitar/DUtff/

it's called callbackKey for JSONP. read the doc.

here's a flickr class that extends jsonp that i wrote (change api pls):

// the class
Request.flickr = new Class({
    Extends: Request.JSONP,
    options: {
        callbackKey: "jsoncallback",
        url: "http://www.flickr.com/services/rest/?",
        log: true
    },
    initialize: function(params, options) {
        this.parent(options);
        this.options.url = this.options.url + Object.toQueryString(params);
    },
    success: function(data, script) {
        this.parent(data, script);
    },
    imageURL: function(obj) {
        return "http://farm{farm}.static.flickr.com/{server}/{id}_{secret}.jpg".substitute(obj);
    }
});

// example on how to use
new Request.flickr({
    format: 'json',
    api_key: "e7df6c74d2545f55414423463bf99723", // your api here
    per_page: 4,
    tags: "mountains",
    method: "flickr.photos.search"
}, {
    onSuccess: function(data) {
        target = document.id("action");
        var self = this;
        data.photos.photo.each(function(el) {
            new Asset.image(self.imageURL(el), {
                onload: function() {
                    this.inject(target);
                }
           });
        });
    }
}).send();

To completely override the callback and pass on an external function (needs to be global):

window.foo = function(data) {
    console.log(data);
};


Request.flickr = new Class({
    Extends: Request.JSONP,
    options: {       
        url: "http://www.flickr.com/services/rest/?jsoncallback=foo&",
        log: true
    },
    initialize: function(params, options) {
        this.parent(options);
        this.options.url = this.options.url + Object.toQueryString(params);
    },
    success: function(data, script) {
        this.parent(data, script);
    },
    imageURL: function(obj) {
        return "http://farm{farm}.static.flickr.com/{server}/{id}_{secret}.jpg".substitute(obj);
    }
});

// example on how to use
new Request.flickr({
    format: 'json',
    api_key: "e7df6c74d2545f55414423463bf99723",
    // your api here
    per_page: 4,
    tags: "mountains",
    method: "flickr.photos.search"
}).send();

the above code will run the function foo, passing on the JSON object to it. http://jsfiddle.net/dimitar/DUtff/

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