-1

我正在尝试使用 json_encode 将数组从 PHP 传递到 javascript

但是当我提醒这些值时,我只看到“对象对象等”

当我 var_dump 它时,我看到了实际的数组,但它没有在警报中显示它们

任何帮助,将不胜感激

问候

这是 var_dump

array(1) {
  [0]=>
  array(2) {
    ["id"]=>
    string(19) "3.0268"
    ["postcode"]=>
    string(137) "hello"
  }
}

array(2) {
  [0]=>
  array(2) {
    ["id"]=>
    string(19) "3.0268070455319E+17"
    ["postcode"]=>
    string(137) "ECMWF continues its flip-flopping, still a temp drop next week & #snow risk but then no rise, http://t.co/tBlg9Ihs #ukweather #uksnow"

} 代码

<?php

 $con =  mysql_connect('localhost', 'root', '');
    mysql_select_db('test');

   $result = mysql_query("SELECT * FROM address");

$arr = array();
while($row = mysql_fetch_assoc($result)) {
    $arr[] = $row; 

}

?>

<script>

var test = <?php echo json_encode($arr); ?>;
alert(test);

</script>
4

1 回答 1

5

alert将调用toString()传递给它的内容。你可能想要console.log. test是一个对象,这是alert默认情况下打印的对象。

例子:

alert({a:1,b:2}) // => [object Object]
({a:1,b:2}).toString() // => "[object Object]"
于 2013-02-22T19:39:36.260 回答