简体   繁体   English

在AJAX调用之后访问对象的属性

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

I do an ajax call and I get the data back from the controller in this form. 我进行了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

This might seem foolish, but I cannot figure out how on earth would I access the cat_id and title properties in each of the arrays! 这看起来很愚蠢,但是我无法弄清楚我将如何访问每个数组中的cat_idtitle属性!

Any help, guys? 大家有帮助吗?

Thanks. 谢谢。

you can just use object notation for accessing them: 您可以使用对象符号来访问它们:

array[0].cat_id
// returns 2

UPDATE 更新

The returned format from your server has to be JSON , it should look like this: 您的服务器返回的格式必须为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
    }
]

You appear to be getting a PHP var_dump or print_r of the data. 您似乎正在获取数据的PHP var_dump或print_r。 This is not something that is convenient to parse. 这不是方便解析的东西。

I'm not aware of any JavaScript parser for the format, so you might have to write one from scratch. 我不知道该格式的任何JavaScript解析器,因此您可能必须从头开始编写一个。 The question Tutorials for writing a parser with Javascript should give you guidance on that subject. 使用Javascript编写解析器的教程教程应该为您提供有关该主题的指导。

You would be better off editing the server side script to return the data in a more reasonable format such as JSON or XML. 您最好编辑服务器端脚本以更合理的格式(例如JSON或XML)返回数据。

It seems like you are using JSON to send and receive data. 似乎您正在使用JSON发送和接收数据。

So you can access the array object and use: 因此,您可以访问数组对象并使用:

yourArrayObject[indexYouWantToAccess].cat_id

Are you using mysql_fetch_object to get data from the database? 您是否正在使用mysql_fetch_object从数据库中获取数据? Try using mysql_fetch_assoc or mysql_fetch_array . 尝试使用mysql_fetch_assocmysql_fetch_array

Also, the response doesn't seem to be a valid json response, as json_encode works properly on both mysql_fetch_object and mysql_fetch_assoc/array . 另外,该响应似乎不是有效的json响应,因为json_encodemysql_fetch_objectmysql_fetch_assoc/array上均可正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM