簡體   English   中英

getJSON或PHP問題

[英]Problem with getJSON or PHP

Display.php

<?php
include 'sql.php';
$dataget = mysql_query("SELECT `user`,`message`,`timestamp` FROM `messages`");
$arr = array();
while ($dataarr = mysql_fetch_assoc($dataget)){
    $arr[] = $dataarr;
}
echo json_encode($arr);
?>

Index.php

<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $.getJSON('display.php', function(data) {
            alert(data.0.user);
        });
    });
</script>

試圖提醒data.0.user

問題是您不能擁有. 后面跟一個數字(在數字上下文中保存)。 您需要使用數組查找語法。

var o = {0:"foo"}
o.0 // SyntaxError

另一方面:

var o = {0:"foo"}
console.log(o[0])//foo

此外,還有一個獨立的數字,后跟一個. 需要后跟另一個數字(或非可變字符)。 (例如: a0后面可以跟一個.1后面也可以,但是1.必須后面跟一個數字)

在Javascript中使用data[0].user

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM