【问题标题】:Get JSON data from PHP function using AJAX使用 AJAX 从 PHP 函数获取 JSON 数据
【发布时间】:2023-01-16 22:10:01
【问题描述】:

我想使用 AJAX 将来自 php 函数的数据发送到我的 HTML 页面,我的函数如下所示:

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

我的 javascript 脚本看起来像:

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

执行代码后,我会在控制台中收到一条 'success' 消息,但不会收到数据。 那么,在这种情况下如何获取 JSON 数据呢?

【问题讨论】:

  • 你确定你在定义它之后打电话给getFeed吗?
  • 无需对每个项目进行编码。就json_encode($articles)
  • @HassanAhmed 我不叫它,我应该如何在我的 javascript 脚本中做
  • 如果你在浏览器中调用“/rss/core/inc/rssnews.inc.php”,你会得到想要的结果吗?
  • 你不能,你必须从你的服务器端调用它,换句话说,从你的 php 文件

标签: javascript php jquery ajax


【解决方案1】:

像这样更改脚本

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

在你的“rssnews.inc.php”文件中写下这段代码

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

【讨论】:

  • 谢谢你。我想知道如何调用该函数以及在哪里调用?
  • 非常感谢,这真的很有效,如果可以的话,请向我解释发生了什么
【解决方案2】:

如果你想从你的方法 [getFeed] 中查看所有 JSON 数据,你可以返回值而不是回显它。

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

然后在你的 JS 中,你可以使用 $.parseJSON 来查看结果。

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

你会得到这样的结果:

希望这对你有帮助

【讨论】:

  • 谢谢你。我收到以下错误:Uncaught SyntaxError: JSON.parse (<anonymous>) 在 Function.n.parseJSON (jquery.min.js:4) 在 Object.success (test.php:33) 在i (jquery.min.js:2) 在 Object.fireWith [as resolveWith] (jquery.min.js:2) 在 y (jquery.min.js:4) 在 XMLHttpRequest.c (jquery.min.js:4) )
  • 你试过先控制{data}吗?并确保您已使用“return json_encode($articles);”返回所有数据。来自 PHP。
【解决方案3】:

您的 javascript 函数应如下所示

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

【讨论】:

    猜你喜欢
    • 2018-07-07
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多