如何在javascript上跨域请求xml

How to request cross-domain for xml on javascript

本文关键字:请求 xml javascript      更新时间:2023-09-26

我想在我的网站上显示雅虎新闻源,所以我需要阅读雅虎rss。

如何使用javascript请求它?"Access Control Allow Origin"会放过它吗?

XML不会像那样跨站点运行。您可以使用类似Yahoo Pipes的东西将其转换为json。

使用此管道http://pipes.yahoo.com/pipes/pipe.info?_id=DJEg41Ac3BG8IAI2E5PZnA并使用url:http://news.yahoo.com/rss/路径.channel.item 进行配置

您可以通过获取"Get as json"链接来进行json输出,该链接提供以下url:http://pipes.yahoo.com/pipes/pipe.run?_id=DJEg41Ac3BG8IAI2E5PZnA&amp_render=json&path=channel.item&url=http%3A%2F%2Fnews.yahoo.com%2Frss%2F

这是一个使用管道的jsfiddlehttp://jsfiddle.net/5KM4X/

如果你不想依赖Pipes,你可以编写一个本地服务器端脚本,通过代理远程url并公开它的输出,它什么都不做。使用PHP,您可以有一个名为的文件

getFeed.php

<?php
header('Content-type: application/xml');
echo file_get_contents("http://news.yahoo.com/rss");

然后访问它(在这里使用jquery)

 $.get('getFeed.php', function(xml){
 ....