[英]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_id
和title
属性!
大家有帮助吗?
谢谢。
您可以使用对象符号来访问它们:
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_assoc
或mysql_fetch_array
。
另外,该响应似乎不是有效的json响应,因为json_encode
在mysql_fetch_object
和mysql_fetch_assoc/array
上均可正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.