I can't figure out how to solve this problem so I'm asking you to help me with it.
I'm trying to parse RSS feed to JSON, but due to XSS problems it can't be done with JS (I'm not using jQuery so don't recommend me plugins). I thought I'll do this with PHP help and wrote some code to get url, parse it, some loops etc. and it returns proper JSON object with array inside of it and it can be seen in Chrome DevTools under Network section.
Problem is that JavaScript shows me that response text is undefined. I've been trying to solve this for 3 hours and I can't figure it out.
Here's the JS code:
this.fetchNews = function ( feed ) {
var method = 'GET';
var url = 'feed.php';
var target = encodeURIComponent( feed );
var params = '?target=' + target;
var request = new XMLHttpRequest();
request.open( method, url + params, true );
request.setRequestHeader( 'Content-Type', 'application/json; charset=utf-8' );
request.onload = function () {
if ( request.readyState === 4 ) {
if ( request.status === 200 ) {
console.error( this.reponseText );
var list = this.reponseText;
for ( var i in list ) {
i.push( self.news.list );
}
console.error( self.newst.list );
}
}
};
request.send( null );
};
this.fetchNews.call( this, 'http://phys.org/rss-feed/&num=20' );
And here's PHP:
$url = $_GET['target'];
$xml = file_get_contents( $url );
$feed = simplexml_load_string( $xml );
$output = array(
'items' => array()
);
$items = $feed->channel[0];
$i = 0;
foreach ( $items->item as $item ) {
$title = $item->title;
$title = (String)$title;
$output['items'][$i++] = $title;
}
echo json_encode( $output );
this.reponseText
is undefined, if so posting the PHP would probably be a good idea ? – adeneo Mar 8 at 18:17request.onload
withrequest.onreadystatechange
and move theopen()
after the function, just abovesend()
. Also make sure the PHP actually outputs something, for instance try hardcoding a small JSON string as the returned data to see that it works – adeneo Mar 8 at 18:31