2012-10-01 28 views
0

我做了一個ajax調用,並以此形式從控制器中獲取數據。在AJAX調用後訪問對象的屬性

Array 
(
[0] => stdClass Object 
    (
     [cat_id] => 2 
     [title] => Clothes 
     [description] => Clothing for men and women 
     [active_coupon_counter] => 0 
     [store_id] => 1 
    ) 

[1] => stdClass Object 
    (
     [cat_id] => 17 
     [title] => Designer 
     [description] => ;description 
     [active_coupon_counter] => 0 
     [store_id] => 1 
    ) 

[2] => stdClass Object 
    (
     [cat_id] => 24 
     [title] => Fashion 
     [description] => ;description 
     [active_coupon_counter] => 0 
     [store_id] => 1 
    ) 

) 
Array 

這似乎是愚蠢的,但我無法弄清楚如何在地球上我會訪問每個陣列的cat_idtitle屬性!

任何幫助,傢伙?

謝謝。

回答

0

你可以只使用對象符號來訪問他們:

array[0].cat_id 
// returns 2 

UPDATE

從服務器返回的格式必須是JSON,它應該是這樣的:

[ 
    { 
     "cat_id": 2, 
     "title": "Clothes", 
     "description": "Clothing for men and women", 
     "active_coupon_counter": 0, 
     "store_id": 1 
    }, 
    { 
     "cat_id": 17, 
     "title": "Designer", 
     "description": ";description", 
     "active_coupon_counter": 0, 
     "store_id": 1 
    }, 
    { 
     "cat_id": 24, 
     "title": "Fashion", 
     "description": ";description", 
     "active_coupon_counter": 0, 
     "store_id": 1 
    } 
] 
+0

array [i]正在返回A,r,r,a,y .... – HarshithaShankar

+1

您能向我們展示您的實際程序嗎?你的返回數據如何顯示 – epoch

+0

這是它的樣子。我做了一個'提醒',這顯示出來了。 – HarshithaShankar

-1

看起來你正在使用JSON來發送和接收數據。

這樣你就可以訪問數組對象,並使用:

yourArrayObject[indexYouWantToAccess].cat_id 
+0

array [i]返回A,r,r,a,y ... 因此array [i] .cat_id返回undefined .. – HarshithaShankar

+1

「看起來你正在使用JSON發送和接收數據。」 - 問題中的數據看起來不像JSON! – Quentin

+0

你可以把代碼放在你從控制器接收js數據的地方嗎? – Bardo

0

你似乎是獲取數據的PHP的var_dump或print_r的。這不是很方便解析的東西。

我不知道任何JavaScript解析器的格式,所以你可能不得不從頭開始編寫一個。 Tutorials for writing a parser with Javascript這個問題應該會爲您提供有關該主題的指導。

您最好編輯服務器端腳本以更合理的格式返回數據,例如JSON或XML。

-1

您是否在使用mysql_fetch_object從數據庫中獲取數據?嘗試使用mysql_fetch_assocmysql_fetch_array

此外,該響應似乎不是有效的json響應,因爲json_encodemysql_fetch_objectmysql_fetch_assoc/array上均正常工作。