开发者

JSONP request to StackOverflow with MooTools is not working

开发者 https://www.devze.com 2022-12-21 19:50 出处:网络
I\'m trying to build a custom StackOverflow badge using JSONP and MooTools. Here is the code: new Request.JSONP(\'http://stackoverflow.com/users/flair/166325.json\', {

I'm trying to build a custom StackOverflow badge using JSONP and MooTools. Here is the code:

new Request.JSONP('http://stackoverflow.com/users/flair/166325.json', {
  onComplete: function(data) {
    console.log(data);
  }
}).request();

However, I always get back this message:

RequestJSONPrequest_maprequest_0 is not defined

I'm wondering if this is a problem with the response from StackOverflow since requests to other services with开发者_StackOverflow社区 JSONP work fine for me.


found a way around it: http://www.jsfiddle.net/CRdr6/1/

by passing on callbackKey: "callback=myfunc&foo" to the Request.JSONP class (it's not escaped properly) you can use myfunc as a global function to handle the callback and go around the stripped .

Request.stackoverflow = new Class({
    Extends: Request.JSONP,
    options: {
        log: true,
        url: "http://stackoverflow.com/users/flair/{user}.json",
        callbackKey: "callback=myfunc&foo"
    },
    initialize: function(user, options) {
        this.parent(options);
        this.options.url = this.options.url.substitute({user: user});
    },
    success: function(data, script) {
        this.parent(data, script);
    }
});


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

new Request.stackoverflow(166325).send();


I ended up creating the function that StackOverflow ends up calling (without the dots):

var StackOverflow = Class.refactor(JsonP, {
  getScript: function(options) {
    var index = Request.JSONP.counter;
    var script = this.previous(options);
    eval("RequestJSONPrequest_maprequest_" + index + " = Request.JSONP.request_map['request_' + index];");
    return script;
  }
});
0

精彩评论

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