1

I need to get feed from third party with jQuery or javascript. I have this code in jQuery to read the rss xml feed in jQuery

$.ajax({
    type: "GET",
    url:'https://www.thirdpartysite.com/feed.xml', 
    dataType: "jsonp",
    jsonp: false,
    jsonpCallback: 'getFeed',
    headers: { "Access-Control-Allow-Origin":"*",},                

    success: function(xml) {
    console.log("success");
    console.log(xml);
    }   
});
function getFeed(val){
    console.log(val);
}

This is giving me "Uncaught SyntaxError: Unexpected token <" error in my console. However the following codes works fine (note cross domain feed access)

$.ajax({
    type: "GET",
    url:'http://mytestsite.com/myfeed.php',   
    dataType: "jsonp",
    jsonp: false,
    jsonpCallback: 'getFeed',
    headers: { "Access-Control-Allow-Origin":"*",},                

    success: function(xml) {
    console.log("success");
    console.log(xml);
    }   
});
function getFeed(val){
    console.log(val);
}

The code in myfeed.php is as below:

echo 'getFeed("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><rss version=\"2.0\"><channel><title>W3Schools Home Page</title><link>https://www.w3schools.com</link><description>Free web building tutorials</description><item><title>RSS Tutorial</title><link>https://www.w3schools.com/xml/xml_rss.asp</link><description>New RSS tutorial on W3Schools</description></item><item><title>XML Tutorial</title><link>https://www.w3schools.com/xml</link><description>New XML tutorial on W3Schools</description></item></channel></rss>");';

It seems that we need to add the callback function while returning the the xml. However I don't have access to add that callbackfunction

Is there a way to make it work for xml rss feed.

Any help will be appreciated.

Thanks

user75472
  • 1,277
  • 4
  • 28
  • 53
  • 1
    This [answer](http://stackoverflow.com/a/19956298/4092887) suggest search another way to make your request _(due to CORS policy)_. You can also make a Google search with these keywords _(if you didn't made yet)_: `Uncaught SyntaxError: Unexpected token < +xml`. It would be great if the response could be returned as string and converted to XML or json, using [JSON.stringify()](https://www.w3schools.com/js/js_json_stringify.asp)... – Mauricio Arias Olave Mar 24 '17 at 17:56

1 Answers1

0

You can do something like this


    var xml = "&ltrss version='2.0'&gt&ltchannel&gt&lttitle&gtRSS Title&lt/title&gt&lt/channel&gt&lt/rss&gt",
    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc ),
    $title = $xml.find( "title" );

Gohel Dhaval
  • 820
  • 1
  • 8
  • 12