Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 );
share|improve this question
    
Don't bother yourself with that type-o in last console.error, it doesn't matter at all. And I'm using error because I have a lot of console.logs and I'm so lazy I can't make myself delete them :) –  szamantg Mar 8 at 18:14
    
You never parse the JSON to an object, not sure how you'd iterate a string, but are you saying the first log with this.reponseText is undefined, if so posting the PHP would probably be a good idea ? –  adeneo Mar 8 at 18:17
    
Also, this looks like it's open to attacks, just opening the console and calling the function with a malicious URL would download anything to the server, and wether that is an issue or not depends on what you do with the downloaded content, but be aware. –  adeneo Mar 8 at 18:20
    
In this case I don't really care about secutiry, because it will sit on my local server on Raspberry Pi, I'm making a Magic Mirror which I want to show me news from RSS channels. PHP added above :) –  szamantg Mar 8 at 18:28
    
Try replacing request.onload with request.onreadystatechange and move the open() after the function, just above send(). 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

1 Answer 1

up vote 0 down vote accepted

Yeeeez I'm an idiot, I wrote this.reponseText instead of this.responseText. Sorry for troubles...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.