[英]JSONP request to StackOverflow with MooTools is not working
我正在嘗試使用JSONP和MooTools構建自定義的StackOverflow徽章。 這是代碼:
new Request.JSONP('http://stackoverflow.com/users/flair/166325.json', {
onComplete: function(data) {
console.log(data);
}
}).request();
但是,我總是得到以下信息:
RequestJSONPrequest_maprequest_0 is not defined
我想知道這是否是StackOverflow響應的問題,因為使用JSONP對其他服務的請求對我來說很好。
找到了解決方法: http : //www.jsfiddle.net/CRdr6/1/
通過將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();
我最終創建了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.