繁体   English   中英

如何使用JavaScript和Ajax从天气RSS提要中获取数据?

[英]How to get data from a weather RSS feed using javascript and ajax?

$.ajax({ 
    type: 'GET',
    url: 'weatherProxy.php',
    dataType: 'xml',
    success: function (xml){
        titles=this.responseXML.getElementsByTagName('title')
        for(i=0; j <titles.length; i++){
            out+= titles[i].childNodes[0].nodeValue + '<br>'                    
        }
    }
});

weatherProxy.php

if (isset($_GET['http://open.live.bbc.co.uk/weather/feeds/en/2645425/3dayforecast.rss'])){
header('Content-Type: text/xml');
echo file_get_contents($_GET['http://open.live.bbc.co.uk/weather/feeds/en/2645425/3dayforecast.rss']);

我试图通过ajax get请求从url中获取信息,我通过代理传递rss feed以防止出现“ No access control allow origin”错误。 目前无法正常工作是在想我是否做错了什么。 我需要将RSS feed上的每个标题添加到数组标题。

删除$_GET ...您使用不正确

尝试:

header('Content-Type: text/xml');
echo file_get_contents('http://open.live.bbc.co.uk/weather/feeds/en/2645425/3dayforecast.rss');

然后在ajax成功中,您可以使用jQuery方法简化解析

$(xml).find('item').each(function(){
   var title = $(this).find('title').text();
   console.log(title)
})

尝试了您的示例,由于您调用的是HTTP而不是https,因此我收到了混合内容警告。

尝试这个:

$.ajax({
  "url": "https://open.live.bbc.co.uk/weather/feeds/en/2645425/3dayforecast.rss",
  "method": "GET"
}).done(function (response) {
  console.log(response);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM