0

The Problem is, the json file is not showing. Im using Xampp as local Server.

php file:

$array = array(
  array(
    "title" => "Erster Eintrag",
    "description" => "Description",
    "link" => "http://",
    "pubDate" => "02.07.2015"
  ),
  array(
    "title" => "Zweiter Eintrag",
    "description" => "Description",
    "link" => "http://",
    "pubDate" => "02.07.2015"
  )      
);
echo json_encode($json);

html file:

$ajax({
url:'localhost/uebung/staticfeed.php',
type:'POST',
data:'data',
dataType: 'json',
success: function(result){
  $('#feeds').html(result[0]);
}
})
Mago99
  • 53
  • 2
  • 10
  • Do you json_encode the php before echoing or what you're doing ? – Jesper Jul 02 '15 at 13:30
  • What happens when you load `localhost/uebung/staticfeed.php`? If nothing, then it's your server config. If you get back an array, you need to `json_encode()` the data, and set the header to the json type. – Samsquanch Jul 02 '15 at 13:31
  • im using json_encode(). With localhost/uebung/staticfeed.php i get the json perfectly shown. – Mago99 Jul 02 '15 at 13:34
  • I'm thinking @Lylo might have the right answer then. – Samsquanch Jul 02 '15 at 13:41

4 Answers4

1

Use json_encode in PHP

Returns a string containing the JSON representation of value.

Use $array to json_encode instead of $json.

echo json_encode($array);
///             ^^^^^^^^^
Tushar
  • 85,780
  • 21
  • 159
  • 179
1

JavaScript use $.ajax and then full URL.

$.ajax({
url:'http://localhost/uebung/staticfeed.php',
type:'POST',
data:'data',
dataType: 'json',
success: function(result){
  $('#feeds').html(result[0]);
});

You also need to encode your array in your php file.

echo json_encode($array);
Lylo
  • 1,221
  • 1
  • 9
  • 12
1

Change $ajax to $.ajax, this will remove the error in your code, everything else works fine

toy
  • 11,711
  • 24
  • 93
  • 176
Unni Babu
  • 1,839
  • 12
  • 16
1

You are missing JSON headers in your PHP answer, Jquery may interpret your response as plain text or HTML...

Also, you are echoing $json and your array is $array.

Try this for PHP:

header('Content-type: application/json');
$array = [["title" => "Erster Eintrag","description" => "Description","link" => "http://","pubDate" => "02.07.2015"],["title" => "Zweiter Eintrag","description" => "Description","link" => "http://","pubDate" => "02.07.2015"]];
echo json_encode($array);

And this on your HTML:

$.ajax({type: "POST", url: "localhost/uebung/staticfeed.php", data:data, dataType: "json", timeout: 25000, success: function (result) {
 $('#feeds').html('First array:' + result.[0].title + '<br />Seccond array:' + result.[1].title );
}});

You need to select the value INSIDE the array inside [result]...

Solrac
  • 924
  • 8
  • 23
  • In this case, since the `$.ajax()` call explicitly lists `dataType: 'json'`, there should not be risk of jQuery interpreting this as something other than JSON, but I am giving +1 as the OP should provide appropriate Content-type header as you mention. This is just a best practice. – Mike Brant Jul 02 '15 at 17:04
  • Thing is... as we are calling for a json file jQuery can interpret the answer as anything else which ends up beign an error because "is not" JSON. On the other hand if we don't define JSON at the ajax call then jQuery will handle that response the best way it can instead of sending it to error. Thanks. – Solrac Jul 02 '15 at 17:13