views:

307

answers:

2

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 JSONP work fine for me.

A: 

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();
Dimitar Christoff
A: 

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