簡體   English   中英

在AJAX調用之后訪問對象的屬性

[英]Accessing the properties of objects after an AJAX call

我進行了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屬性!

大家有幫助嗎?

謝謝。

您可以使用對象符號來訪問它們:

array[0].cat_id
// returns 2

更新

您的服務器返回的格式必須為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
    }
]

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

我不知道該格式的任何JavaScript解析器,因此您可能必須從頭開始編寫一個。 使用Javascript編寫解析器的教程教程應該為您提供有關該主題的指導。

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

似乎您正在使用JSON發送和接收數據。

因此,您可以訪問數組對象並使用:

yourArrayObject[indexYouWantToAccess].cat_id

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

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

暫無
暫無

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

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