Search code examples
javascriptjsonpmootools

JSONP request to StackOverflow with MooTools is not working


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.


Solution

  • 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();