I want to send data ing from a php function to my HTML page using AJAX, my function look like:
function getFeed() {
$url = '.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?
I want to send data ing 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?
-
are you sure that you are calling
getFeed
after defining it ? – hassan Commented Mar 4, 2017 at 12:49 -
There's no need to encode every item. Just
json_encode($articles)
– u_mulder Commented Mar 4, 2017 at 12:49 - @HassanAhmed I don't call it, how should I do in my javascript script – Ha KiM's 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 ? – Dan Ionescu 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 – hassan Commented Mar 4, 2017 at 12:52
3 Answers
Reset to default 2change 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);
}
}
});
});