JQuery 插件帮助
我正在尝试开发一个 JQuery 插件,它将访问外部 XML 提要并显示结果。到目前为止,这是我所拥有的:
HTML 标头包括
<script language="javascript" src="jquery.rss.js" type="text/javascript"></script>
JQuery Ready
<script type="text/javascript">
$(document).ready(function() {
$("#rss").rss({count:6,loading_text:"loading"});
});
</script>
插件 (jquery.rss.js)
(function($) {
$.fn.rss = function (o) {
var s = {
count: 6,
loading_text: null,
};
if(o) $.extend(s,o);
return this.each (function () {
var list = $('<ul class="rss">').appendTo(this);
var loading = $('<p class="desc"><center><img src="loading.gif" height="19" width="18" border="0"><br>'+s.loading_text+'</center></p>');
var items = 0;
var url = 'http://www.example.com/feed.xml;
if (s.loading_text) $(this).append(loading);
$.get(url,{},function(data){
if (s.loading_text) loading.remove();
$('forecastday',data).each(function(i){
var title = $(this).find("title").text();
var description = $(this).find("description").text();
list.append('<li>' + title + ' - ' + description + '</li>');
items++;
if(items == s.count) last;
});
});
});
}
})(jQuery);
一切似乎都正常工作,直到我尝试执行 $.get ,此时似乎没有返回任何内容。我已经使用alert() 验证了从$.get 请求调用了正确的URL。
希望我离得不远,并且 JQuery 大师可以指出我哪里出错了。预先感谢您的帮助!
I'm trying to develop a JQuery plug-in that will access an outside XML feed and display the results. Here is what I have so far:
HTML Header Include
<script language="javascript" src="jquery.rss.js" type="text/javascript"></script>
JQuery Ready
<script type="text/javascript">
$(document).ready(function() {
$("#rss").rss({count:6,loading_text:"loading"});
});
</script>
Plugin (jquery.rss.js)
(function($) {
$.fn.rss = function (o) {
var s = {
count: 6,
loading_text: null,
};
if(o) $.extend(s,o);
return this.each (function () {
var list = $('<ul class="rss">').appendTo(this);
var loading = $('<p class="desc"><center><img src="loading.gif" height="19" width="18" border="0"><br>'+s.loading_text+'</center></p>');
var items = 0;
var url = 'http://www.example.com/feed.xml;
if (s.loading_text) $(this).append(loading);
$.get(url,{},function(data){
if (s.loading_text) loading.remove();
$('forecastday',data).each(function(i){
var title = $(this).find("title").text();
var description = $(this).find("description").text();
list.append('<li>' + title + ' - ' + description + '</li>');
items++;
if(items == s.count) last;
});
});
});
}
})(jQuery);
Everything appears to be working correctly up until I try to do the $.get at which point nothing appears to be returned. I've verified by using alert() that the correct URL is being called from the $.get request.
Hopefully I'm not far off and a JQuery guru can point out where I'm going wrong. Thanks in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能跨域进行ajax请求。要么开发一个服务器端代理(部署在同一主机上)将您的请求路由到 wunderground,要么寻找支持 JSONP 的 API。
另请参阅 - 根据经度和纬度获取天气的 API坐标
You can't do ajax requests cross-domain. Either develop a server-side proxy (deployed on the same host) that routes your request to wunderground or look for API that supports JSONP.
See also - API to get weather based on longitude and latitude coordinates
查看这个 jQuery 插件:jdigiclock。它使用代理来获取和解析 XML 数据并将其提供给 jQuery 脚本。我认为,您正在寻找的是同一件事。
Check out this jQuery plugin: jdigiclock. It uses proxy to get and parse the XML data and serves it to the jQuery script. I think, that it is the same thing, you are looking for.