0

I want to send data coming from a php function to my HTML page using AJAX, my function look like:

     function getFeed() {
        $url = 'http://feeds.bbci.co.uk/news/rss.xml?edition=int#';
        $content = file_get_contents($url);
        $data = simplexml_load_string($content);
        $articles= array();

        foreach( $data->channel->item as $item){

            $articles[]=array(
                'title'         =>  (string)$item->title,
                'description'   =>  (string)$item->description,
                'link'          =>  (string)$item->link,
                'Date'          =>  (string)$item->pubDate,
            );
        }

        foreach($articles as $article){
        echo json_encode($article['title']);
        }
    }

my javascript script look like:

$(function(){
    $.ajax({
        type:'GET',
        url: '/rss/core/inc/rssnews.inc.php',
        success: function (data){
        console.log('success',data);
        }
    });
});

Once I execute the code, I get a 'success' message in the console, but not the data. So, how can I get the JSON data in this case?

18
  • are you sure that you are calling getFeed after defining it ? Commented Mar 4, 2017 at 12:49
  • There's no need to encode every item. Just json_encode($articles) Commented Mar 4, 2017 at 12:49
  • @HassanAhmed I don't call it, how should I do in my javascript script Commented Mar 4, 2017 at 12:51
  • if you call "/rss/core/inc/rssnews.inc.php" in your browser, do you get the desired results ? Commented Mar 4, 2017 at 12:52
  • you can't , you have to call it from your -server side- , in another words from your php file Commented Mar 4, 2017 at 12:52

3 Answers 3

2

change script like this

$(function(){
    $.ajax({
        type:'GET',
        url: '/rss/core/inc/rssnews.inc.php?function=getFeed',
        success: function (data){
        console.log('success',data);
        }
    });
});

in your "rssnews.inc.php" file write this code

if(isset($_GET['function']) && $_GET['function'] !=''){
    $result = $_GET['function']();
    echo json_encode($result);
}
function getFeed() {
    $url = 'http://feeds.bbci.co.uk/news/rss.xml?edition=int#';
    $content = file_get_contents($url);
    $data = simplexml_load_string($content);
    $articles= array();

    foreach( $data->channel->item as $item){

        $articles[]=array(
            'title'         =>  (string)$item->title,
            'description'   =>  (string)$item->description,
            'link'          =>  (string)$item->link,
            'Date'          =>  (string)$item->pubDate,
        );
    }

    $articalesArr = array();
    foreach($articles as $article){
        array_push($articalesArr, $article['title']);    
    }
    return $articalesArr;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I want to know just how to call the function and where ?
Thank you so much this is really work, if you can, just explain to me what's happen
2

If you want to see the all of JSON data from your method [getFeed], You can return the value instead of echoing it.

 function getFeed() {
    $url = 'http://feeds.bbci.co.uk/news/rss.xml?edition=int#';
    $content = file_get_contents($url);
    $data = simplexml_load_string($content);
    $articles= array();

    foreach($data->channel->item as $item) {

        $articles[]=array(
            'title'         =>  (string)$item->title,
            'description'   =>  (string)$item->description,
            'link'          =>  (string)$item->link,
            'Date'          =>  (string)$item->pubDate,
        );
    }

    return json_encode($articles);
}

Then in your JS, You can use the $.parseJSON to see the results.

$.ajax({
    type:'GET',
    url: '/rss/core/inc/rssnews.inc.php',
    success: function (data) {
        var oJsonResponse = $.parseJSON(data);
        console.log(oJsonResponse);
    }
});

You would get the results like this:

enter image description here

Hope this helps for you

2 Comments

Thank you. I get the following error: Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at Function.n.parseJSON (jquery.min.js:4) at Object.success (test.php:33) at i (jquery.min.js:2) at Object.fireWith [as resolveWith] (jquery.min.js:2) at y (jquery.min.js:4) at XMLHttpRequest.c (jquery.min.js:4)
Have you try to console the {data} first? And make sure you've return all of the data using "return json_encode($articles);" from PHP.
0

Your javascript function should be as below

$(function(){
    $.ajax({
        type:'GET',
        url: '/rss/core/inc/rssnews.inc.php',
        success: function (data){
            for(var i=0;i<data.length;i++){
                console.log(data[i].title);
             }
        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.