JSON回调函数仅适用于文本文件

时间:2011-03-09 19:04:04

标签: php jquery json

有没有人知道为什么回叫只适用于文本文件

PHP

<?php include("alan.php"); ?>

<?php
$rows=array();
mysql_select_db('news') or die(mysql_error());
//echo "Connected to Database";<?php 

$result = mysql_query("SELECT * FROM photos") 
or die(mysql_error());  

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_assoc( $result )) {    
    $rows[]=$row;
} 
echo json_encode($rows);
?>

输出:

[{"name":"photo1.jpg","id":"1"},{"name":"photo2.jpg","id":"2"},{"name":"photo3.jpg","id":"3"},{"name":"photo4.jpg","id":"4"}]

JavaScript的:

$(document).ready(function() { 
    $('#photos').click(function(){
        $.getJSON('photo_get.php',function(data){ /**Changing this to a text file works ????**/
            $.each(data, function(key, val) {
                alert("Data"    + val.name);
            });
        });
    });
});

1 个答案:

答案 0 :(得分:3)

您是否尝试在php页面中设置内容类型?

<?php
  header("Content-Type: application/json");
?>