jQuery显示PHP中的数组(json编码)

jQuery显示PHP中的数组(json编码),php,jquery,arrays,json,Php,Jquery,Arrays,Json,我试图用jQuery显示一个数组,但它没有显示我想要的任何内容:p 所以,我在谷歌上坐了一个小时后,在这里问了一个问题,但仍然不知道我做错了什么 我的数组是: if (isset($_POST['imageID'])) { $array = array(); list($width, $height, $type, $attr) = getimagesize("../img/libary/".$_POST['imageID'].".JPG");

我试图用jQuery显示一个数组,但它没有显示我想要的任何内容:p

所以,我在谷歌上坐了一个小时后,在这里问了一个问题,但仍然不知道我做错了什么

我的数组是:

if (isset($_POST['imageID'])) { 
            $array = array();
            list($width, $height, $type, $attr) = getimagesize("../img/libary/".$_POST['imageID'].".JPG");

            $array["dimension"] = $width.' x '.$height;
            $array["filesize"] = formatBytes(filesize("../img/libary/".$_POST['imageID'].".jpg"),0);
            $array["extensions"] = strtoupper(pathinfo("../img/libary/".$_POST['imageID'].".jpg", PATHINFO_EXTENSION));
            $array["filename"] = $_database->query("SELECT * FROM imglibary WHERE imageID='".$_POST['imageID']."' LIMIT 1")->fetch_object()->name; 
            $array["fileurl"] = $_SERVER['DOCUMENT_ROOT'].'/img/libary/'.$_POST['imageID'].'.JPG';

            echo json_encode($array);
        }
和jQuery代码:

$.post("jQuery.post.php", {imageID: imageID}, "json").done(function(data) { 
 // What to do here to get it to work? 
  });
希望你们有个答案,我都不知道了:(

未能显示“数据”中的答案:


使用
$.parseJSON
解析从服务器返回的数据,除非您从PHP页面发送
应用程序/json

如果要开始从PHP页面发送正确的JSON标题,请将其添加到页面顶部,默认情况下,它设置为
text/html
,这就是需要解析数据的原因:

header('Content-type: application/json');
然后,它就像
console.log(data.dimension)
一样简单,不需要任何解析。只要将
json
设置为
dataType
,jQuery就会自动为您解析数据

如果您不想/无法发送正确的标题,或者不确定它们是什么,请使用
parseJSON()

带有parseJSON的示例:

$.post("jQuery.post.php", {imageID: imageID}, "json").done(function(data) { 
   var d = $.parseJSON(data);
   console.log(d.dimension);
});

正如奥尔德林所说,你想解析数据。我想当你将JSON作为数据类型时,它会自动解析数据?@Papazzokid,如果它是从php以
text/html
的形式发送的话,就不会了。@Darren-谢谢,这很有意义。但是,你怎么知道OP的php头被设置为
text/html
而不是
application/JSON?@狗仔队否则它会被解析;-)我只有在这样做的时候才得到未定义。
$.post("jQuery.post.php", {imageID: imageID}, "json").done(function(data) { 
   var d = $.parseJSON(data);
   console.log(d.dimension);
});