打开“http://localhost/uebung/index.html”时,只显示HTML / CSS(无法读取JSON)
JSON数据(到目前为止工作):
<?php
$array = array(
array(
"title" => "Erster Eintrag",
"description" => "Hier kommt eine Beschreibung hin!",
"link" => "http://",
"pubDate" => "02.07.2015"
),
array(
"title" => "Zweiter Eintrag",
"description" => "Hier kommt eine Beschreibung hin!",
"link" => "http://",
"pubDate" => "02.07.2015"
)
);
echo json_encode($array);?>
html文件(此文件无法读取json文件):
<script type="text/javascript">
$.ajax({
url:'http://localhost/uebung/staticfeed.php',
type:'POST',
data: val,
dataType: 'json',
success: function(data){
$.each(data, function(key, val){
$('#feeds').append('<div id="' + key + '">' + val.title + ' ' + val.description + ' ' + val.link + ' ' + val.pubDate + '</div>');
});
}
})
</script>
<div id="feeds">dsf
</div>
答案 0 :(得分:3)
写下这样你告诉你的脚本你发送的是JSON
header("Content-Type: application/json", true);
echo json_encode($array);?>
或强>
在迭代之前解码JSON。
success: function(data){
data = $.parseJSON(data);
$.each(data, function(key, val){
....