【发布时间】:2014-07-03 05:52:05
【问题描述】:
我正在编写我的第一个 jQuery 插件并寻求一些帮助,以便在其中发出 JSONP 请求。
通常我在这样的回调函数中设置我的 JSON 数据
saveDataCommunityPartners({
"newsBlockItems": [
{
"title": "title title title",
"img": "item-img.jpg"
},
{
"title": "title title title",
"img": "item-img.jpg"
}
... and so on and so forth
]
})
然后在一个页面上我这样调用.getJSON():
$.getJSON("http://i.cdn.turner.com/nba/nba/.element/media/2.0/teamsites/warriors/json/json-redesign-communitypartners.js?callback=?");
并设置回调函数
function saveDataCommunityPartners(data) {
alert("got data");
}
一切正常。我如何让它在我的插件中工作?如果我像下面那样做,我会收到“未捕获的 ReferenceError:未定义 saveDataCommunityPartners”错误。
(function($) {
$.fn.createNewsBlock = function( options ) {
// Establish defaults
var settings = $.extend({
thisData : {},
pageFilter : "community"
}, options);
$.getJSON("http://i.cdn.turner.com/nba/nba/.element/media/2.0/teamsites/warriors/json/json-redesign-communitypartners.js?callback=?");
function saveDataCommunityPartners(data) {
alert("got data");
}
}(jQuery));
我可以更改插件,或者我如何设置 JSON 文件本身,无论推荐什么。谢谢!
【问题讨论】:
标签: jquery ajax jquery-plugins jsonp getjson