2017-03-04 70 views
0

从PHP函数JSON数据我想将数据从一个PHP函数来发送使用AJAX我的HTML网页,我的功能看起来像:获取使用AJAX

 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数据呢?如果你想看到所有的JSON的

+0

你确定你定义后调用'getFeed'? – hassan

+0

无需编码每个项目。只是'json_encode($ articles)' –

+0

@HassanAhmed我不叫它,我应该怎么做我的JavaScript脚本 –

回答

2

这样

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

谢谢。我想知道如何调用该函数以及在哪里? –

+0

非常感谢,如果可以的话,这真的很有用,只需向我解释发生了什么 –

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

你方法的数据[getFeed],你可以返回值而不是回显它。

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

你会得到的结果是这样的:

enter image description here

希望这有助于你

+0

谢谢。我收到以下错误: 未捕获的SyntaxError:JSON输入的意外结束 在JSON。解析()在Function.n.parseJSON(jquery.min.js:4) 在Object.success(test.php的:33) 在I(jquery.min.js:2) 在Object.fireWith [ (jquery.min.js:2) at y(jquery.min.js:4) at XMLHttpRequest.c(jquery.min.js:4) –

+0

您是否尝试先控制{data}?并确保你已经使用“return json_encode($ articles)”返回所有数据;“来自PHP。 – eeya