简体   繁体   中英

Get JSON data from PHP function using AJAX

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?

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;
}

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:

在此处输入图片说明

Hope this helps for you

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);
             }
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM