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;
}
});
精彩评论