jquery each报 Uncaught TypeError: Cannot use 'in' operator to search for错误
2015-08-13 15:44
681 查看
在写前端的时候用jquery来遍历后台传来的json数组时候遇到这个错误:Uncaught TypeError: Cannot use 'in' operator to search for。后来查到原因是因为:一部分浏览器后端传过来的是json对象,但是我们前端是需要Javascript的对象,所以需要做个转换JSON.parse() or jQuery $.parseJSON
Review a simple jQuery example to loop over a JavaScript array object.
Above code snippet is working fine, prompts the “apple”, “orange” … as expected.
Review below example, declares a JSON string (enclosed with single or double quotes) directly.
In Chrome, it shows following errors in console :
To fix it, converts it to Javascript object via standard
Note
Most web applications will return JSON formatted string directly, you need to convert it to JavaScript object before parse it with jQuery.
参考链接:http://www.mkyong.com/jquery/jquery-loop-over-json-string-each-example/
Review a simple jQuery example to loop over a JavaScript array object.
var json = [ {"id":"1","tagName":"apple"}, {"id":"2","tagName":"orange"}, {"id":"3","tagName":"banana"}, {"id":"4","tagName":"watermelon"}, {"id":"5","tagName":"pineapple"} ]; $.each(json, function(idx, obj) { alert(obj.tagName); });
Above code snippet is working fine, prompts the “apple”, “orange” … as expected.
Problem : JSON string
Review below example, declares a JSON string (enclosed with single or double quotes) directly.var json = '[{"id":"1","tagName":"apple"},{"id":"2","tagName":"orange"}, {"id":"3","tagName":"banana"},{"id":"4","tagName":"watermelon"}, {"id":"5","tagName":"pineapple"}]'; $.each(json, function(idx, obj) { alert(obj.tagName); });
In Chrome, it shows following errors in console :
Uncaught TypeError: Cannot use 'in' operator to search for '156' in [{"id":"1","tagName":"apple"}...
Solution : Convert JSON string to JavaScript object
To fix it, converts it to Javascript object via standard JSON.parse()or jQuery
$.parseJSON.
var json = '[{"id":"1","tagName":"apple"},{"id":"2","tagName":"orange"}, {"id":"3","tagName":"banana"},{"id":"4","tagName":"watermelon"}, {"id":"5","tagName":"pineapple"}]'; $.each(JSON.parse(json), function(idx, obj) { alert(obj.tagName); }); //or $.each($.parseJSON(json), function(idx, obj) { alert(obj.tagName); });
Note
Most web applications will return JSON formatted string directly, you need to convert it to JavaScript object before parse it with jQuery.
参考链接:http://www.mkyong.com/jquery/jquery-loop-over-json-string-each-example/
相关文章推荐
- 使用jquery.fileDownload.js插件导出excel
- Jquery 中 ajaxSubmit使用讲解 j-m 后台登录
- jquery ajax请求后台 的简单例子
- jQuery插件手把手教会(一)
- 对于jQuery性能的一些优化建议
- jQuery库(noConflict)冲突解决机制
- 【jQuery】Jquery.cookie()
- jquery.Validation.js使用手册,案例Demo
- Jquery Web 前端验证框架插件,jquery-validate资料,validation
- jQuery.each方法
- 解析jquery获取父窗口的元素
- jquery关于select框的取值和赋值
- Jquery中animate可以操作css样式属性总结
- jQuery简要dom操作
- jQuery的mouseover和mouseout事件当快速移动 事件延迟触发问题
- Jquery 多选下拉列表插件jquery multiselect
- jQuery实现展开伸缩效果
- jquery处理innerText与textContent
- 详解JavaScript中jQuery和Ajax以及JSONP的联合使用
- 关于jquery中jQuery slideToggle() 方法实现的原理