[英]JSONP request to StackOverflow with MooTools is not working
I'm trying to build a custom StackOverflow badge using JSONP and MooTools. 我正在尝试使用JSONP和MooTools构建自定义的StackOverflow徽章。 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. 我想知道这是否是StackOverflow响应的问题,因为使用JSONP对其他服务的请求对我来说很好。
found a way around it: http://www.jsfiddle.net/CRdr6/1/ 找到了解决方法: 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 .
通过将callbackKey:“ callback = myfunc&foo”传递给Request.JSONP类(无法正确转义),您可以将myfunc用作全局函数来处理回调并解决已剥离的问题
.
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): 我最终创建了StackOverflow最终调用的函数(不带点):
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;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.