Jquery - Uncaught TypeError: Cannot use #39;in#39; operator to search for #39;324#39; in(Jquery - 未捕获的类型错误:无法使用“in运算符来搜索“324)
问题描述
我正在尝试通过 ajax 发送 Get 请求,并以 html 格式输出服务器返回的 json 数据.
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",}]
这是我通过ajax向php文件发送Get请求的代码.当我使用 $.each 方法时,它得到了我在上面显示的错误.
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代码,以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 字符串,而不是一个对象.告诉 jQuery 您希望得到 JSON 响应,它会为您解析它.要么使用 $.getJSON 而不是 $.get,或将 dataType 参数传递给 $.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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Jquery - 未捕获的类型错误:无法使用“in"运算符来搜索“324"
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- PHP - if 语句中的倒序 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01