php Jquery - 未捕获的类型错误:无法使用“in”运算符来搜索“324”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18502101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery - Uncaught TypeError: Cannot use 'in' operator to search for '324' in
提问by Hayato
I'm trying to send a Get request by ajax and output json data that is returned by server in html.
我正在尝试通过 ajax 发送 Get 请求并输出服务器以 html 形式返回的 json 数据。
But, I got this error.
但是,我收到了这个错误。
Uncaught TypeError: Cannot use 'in' operator to search for '324' in
[{"id":50,"name":"SEO"},{"id":22,"name":"LPO",}]
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.
这是我通过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)
})
}
)
})
This is my PHP code that returns query result in json format.
这是我的 PHP 代码,它以 json 格式返回查询结果。
public function actionAjax(){
$parentCatId=$_GET['parentCatId'];
$catData = Category::getTargetCategoryData($parentCatId);
echo CJSON::encode($catData);
Yii::app()->end();
}
json data outputted by this php is like this.
这个php输出的json数据是这样的。
[{"id":50,"name":"SEO"},{"id":22,"name":"LPO",}]
Anyone knows how to fix this problem?
有谁知道如何解决这个问题?
Please help me out. Thanks in advance :)
请帮帮我。提前致谢 :)
回答by Paul
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 $.getJSONinstead of $.get, or pass the dataType argument to $.get
:
您有一个 JSON 字符串,而不是一个对象。告诉 jQuery 你期望一个 JSON 响应,它会为你解析它。要么使用$.getJSON而不是$.get,要么将 dataType 参数传递给$.get
:
$.get(
'index.php?r=admin/post/ajax',
{"parentCatId":parentCatId},
function(data){
$.each(data, function(key, value){
console.log(key + ":" + value)
})
},
'json'
);
回答by HansH
You can also use $.parseJSON(data)
that will explicit convert a string thats come from a PHP script to a real JSON array.
您还可以使用$.parseJSON(data)
它将来自 PHP 脚本的字符串显式转换为真正的 JSON 数组。
回答by doobdargent
If you're fetching JSON, use $.getJSON()so it automatically converts the JSON to a JS Object.
如果您正在获取 JSON,请使用$.getJSON()以便它自动将 JSON 转换为 JS 对象。
回答by LeHill
I fixed a similar error by adding the json dataType like so:
我通过添加 json dataType 修复了类似的错误,如下所示:
$.ajax({
type: "POST",
url: "someUrl",
dataType: "json",
data: {
varname1 : "varvalue1",
varname2 : "varvalue2"
},
success: function (data) {
$.each(data, function (varname, varvalue){
...
});
}
});
And in my controller I had to use double quotes around any strings like so (note: they have to be escaped in java):
在我的控制器中,我必须在任何字符串周围使用双引号(注意:它们必须在 java 中转义):
@RequestMapping(value = "/someUrl", method=RequestMethod.POST)
@ResponseBody
public String getJsonData(@RequestBody String parameters) {
// parameters = varname1=varvalue1&varname2=varvalue2
String exampleData = "{\"somename1\":\"somevalue1\",\"somename2\":\"somevalue2\"}";
return exampleData;
}
So, you could try using double quotes around your numbers if they are being used as strings (and remove that last comma):
因此,如果您的数字被用作字符串(并删除最后一个逗号),您可以尝试在数字周围使用双引号:
[{"id":"50","name":"SEO"},{"id":"22","name":"LPO"}]
回答by TNS MUMET
Use getJSON
使用 getJSON
$.getJSON(
'index.php?r=admin/post/ajax',
{"parentCatId":parentCatId},
function(data){
$.each(data, function(key, value){
console.log(key + ":" + value)
})
});
Detail look here http://api.jquery.com/jQuery.getJSON/