從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的
你確定你定義後調用'getFeed'? – hassan
無需編碼每個項目。只是'json_encode($ articles)' –
@HassanAhmed我不叫它,我應該怎麼做我的JavaScript腳本 –