irmorteza
irmorteza

Reputation: 1654

How to request cross-domain for xml on javascript

I want to show yahoo news feeds in my website, so I need to read Yahoo rss.

How to request it, with javascript? Do "Access-Control-Allow-Origin" let it go?

Upvotes: 2

Views: 425

Answers (1)

Will
Will

Reputation: 2712

XML won't run cross-site like that. You could convert it to json using something like Yahoo Pipes.

Using this pipe http://pipes.yahoo.com/pipes/pipe.info?_id=DJEg41Ac3BG8IAI2E5PZnA and configuring with url:http://news.yahoo.com/rss/ and path:channel.item

You can make json output by grabbing the "Get as JSON" link that gives you this url: http://pipes.yahoo.com/pipes/pipe.run?_id=DJEg41Ac3BG8IAI2E5PZnA&_render=json&path=channel.item&url=http%3A%2F%2Fnews.yahoo.com%2Frss%2F

And here's a jsfiddle using that pipe http://jsfiddle.net/5KM4X/

If you don't want to rely on Pipes, you could write a local server side script that does nothing by proxy the remote url and expose it's output. Using PHP, you could have a file called

getFeed.php

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

And then access it (using jquery here)

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

Upvotes: 1

Related Questions