加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

有关TypeError: invalid 'in' operand obj的错误

发布时间:2020-12-16 00:57:22 所属栏目:百科 来源:网络整理
导读:由php返回一个json数据 $.ajax({ type: "POST", url: "example.php", dataype: "json", data: { name: "John",location: "Boston" }, success: function (data) { $.each(data,function(key,val) { items.push('li id="' + key + '"' + val + '/li'); }); }

由php返回一个json数据

$.ajax({
type: "POST",
url: "example.php",
dataype: "json",
data: { name: "John",location: "Boston" },
success: function (data) {
$.each(data,function(key,val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
}
});

使用each解析时候,就会报错TypeError: invalid 'in' operand obj。

http://www.bennadel.com/blog/1918-Javascript-s-IN-Operator-Does-Not-Work-With-Strings.htm

看到那篇文章后,发现js的 in对字符串不管用,于是代码修改为:

$.ajax({
type: "POST",
success: function (data) {

obj= $.parseJSON(data);

$.each( obj,val) {
items.push('<li id="' + key + '">' + val.name+ '</li>');
});
}
});

官网对parseJSON的描述是:Takes a well-formed JSON string and returns the resulting JavaScript object.

正好符合问题状况--问题解决!



(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读