jQuery的 - 未捕获的类型错误:不能用“IN”操作符搜索在'324' [英] Jquery - Uncaught TypeError: Cannot use 'in' operator to search for '324' in
问题描述
我试图通过服务器的HTML返回阿贾克斯和输出JSON数据发送GET请求。
I'm trying to send a Get request by ajax and output json data that is returned by server in html.
不过,我得到这个错误。
But, I got this error.
Uncaught TypeError: Cannot use 'in' operator to search for '324' in
[{"id":50,"name":"SEO"},{"id":22,"name":"LPO",}]
这是我的code发送一个GET请求到PHP文件阿贾克斯。 当我使用$。每个方法时,它得到了我发现,在上述错误。
This is my code that sends a Get request to php file by ajax. When I use $.each method, it get the error that I showed in the above.
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 code,返回查询结果以JSON格式。
This is my PHP code that returns query result in json format.
public function actionAjax(){
$parentCatId=$_GET['parentCatId'];
$catData = Category::getTargetCategoryData($parentCatId);
echo CJSON::encode($catData);
Yii::app()->end();
}
通过这个PHP输出
JSON数据是这样的。
json data outputted by this php is like this.
[{"id":50,"name":"SEO"},{"id":22,"name":"LPO",}]
任何人都知道如何解决这个问题呢?
Anyone knows how to fix this problem?
请帮助我。 在此先感谢:)
Please help me out. Thanks in advance :)
推荐答案
您有一个JSON字符串,而不是一个对象。告诉你期待JSON响应jQuery的,它会分析它。无论是使用 $ .getJSON 代替的$.get ,或者通过具体的数据类型参数 $获得
:
You have a JSON string, not an object. Tell jQuery that you expect a JSON response and it will parse it for you. Either use $.getJSON instead of $.get, or pass the dataType argument to $.get
:
$.get(
'index.php?r=admin/post/ajax',
{"parentCatId":parentCatId},
function(data){
$.each(data, function(key, value){
console.log(key + ":" + value)
})
},
'json'
);
这篇关于jQuery的 - 未捕获的类型错误:不能用“IN”操作符搜索在'324'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!