我试图通过ajax发送一个Get请求,并将服务器返回的json数据在html中输出。但是,我收到了这个错误。
Uncaught TypeError: Cannot use 'in' operator to search for '324' in
[{"id":50,"name":"SEO"},{"id":22,"name":"LPO",}]
这是我的代码,通过ajax向php文件发送Get请求。
当我使用$.each方法时,它会显示上述错误。parentCat.on('change', function(e){
parentCatId = $(this).val();
$.get(
'index.php?r=admin/post/ajax',
{"parentCatId":parentCatId},
function(data){
$.each(data, function(key, value){
console.log(key + ":" + value)
})
}
)
})
这是我的PHP代码,它以JSON格式返回查询结果。
public function actionAjax(){
$parentCatId=$_GET['parentCatId'];
$catData = Category::getTargetCategoryData($parentCatId);
echo CJSON::encode($catData);
Yii::app()->end();
}
这个 PHP 输出的 JSON 数据长成这样。
[{"id":50,"name":"SEO"},{"id":22,"name":"LPO",}]
这个问题应该如何解决?
each
语句之前添加一个console.log(data)
并查看控制台中打印的内容吗? - Arun P Johny$.each('selector')...
而不是$('selector').each(...
- Dylan Valade