由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.
正好符合问题状况--问题解决!