2010-10-20 32 views
0

我試圖訪問一個jQuery JSON對象,我看不出什麼毛病witj我的代碼JSON對象,但我得到的,當我嘗試訪問的對象是undefined,我在想,也許這是因爲它是嵌套的方式,但我不確定,這裏是我的代碼,Acessing使用jQuery

function systems(dom_id, id){ 
    $.getJSON("get_systems.php", {uid:id}, function(data){ 
    // here I get the general system info related to the user 
    $.each(data, function(i, json){ 
    //here I get each systems name, related to each system 
    $.getJSON('get_system_name.php', {uid:json.product_id}, function(data){ 
    console.log(data.products); 
    $(dom_id).append('<tr><th>'+data.products+'</th></tr>'); 
    }); 

    }); 

    }); 
}; 

列名與系統中它的名字,被命名爲products,因此我嘗試使用,以獲取名稱data.products,但我得到的是undefined正如我上面提到。

但當我只要登錄data它顯示的對象,它看起來像這樣,[Object { products="this is the product name"}],這是我用它來獲得系統名稱PHP,

get_system_name.php:

<?php 
$uid = $_GET['uid']; 

mysql_connect("localhost", "user", "1234") or die(mysql_error()); 
mysql_select_db("the_DB") or die(mysql_error()); 
$query = mysql_query("SELECT products FROM products WHERE product_id='$uid'") or die(mysql_error()); 

while($array = mysql_fetch_array($query, true)) { 
$string[] = $array; 
} 

$json = json_encode($string); 
echo $json; 
?> 

感謝提前!

+0

有沒有這樣的事,作爲一個「JSON對象」。它可以是JSON字符串,也可以是本機對象。 – 2010-10-20 08:19:29

+0

編輯問題以添加json輸出(通過直接訪問瀏覽器中的url) – 2010-10-20 08:26:00

+0

Thanx,對於所有這些仍然是新東西,所以請原諒我的無知,但感謝您的更正,請問我可以在哪裏學習更多關於這個問題? – Odyss3us 2010-10-20 08:32:28

回答

0

通過該線的外觀: [Object { products="this is the product name"}]

數據實際上是JSON的陣列,而不是一個JSON。所以,如果你沒有

data[0].products

,它應該工作。你可能想看看它爲什麼回來作爲數組不過,除非這是你想要的行爲。

+0

會做,感謝名單。 – Odyss3us 2010-10-20 08:27:08

+0

得到它的工作!它將每個JSON字符串都推送到一個數組中,這是不需要的,因爲我每次只獲取一個名稱,所以我將代碼更改爲僅返回字符串而不是帶有JSON字符串的數組,現在它工作正常!非常感謝! – Odyss3us 2010-10-20 09:26:43

+0

幹得好。我會進一步幫助,但我不知道很多的PHP,所以說除了我知道它被推入一個數組之外的任何東西。 – EMMERICH 2010-10-20 10:17:17