关于ajax返回数据处理

时间:2021-10-26 21:43:38

查看jquery文档,我们知道jquery有很多种Ajax调用方法,下面结合springmvc返回的数据,假设返回
的是data
='{"label":"1","value":[{"id":1,"pid":null,"name":"娱乐"},{"id":101,"pid":1,"name":"音乐"},{"id":112,"pid":1,"name":"卡通"}],"text":""}',
则在如下的ajax调用和回调中,可以参考范例:
$.get("test.do", function(data){
   alert("Data Loaded: " + data);
   var jsonObj = eval('(' + data + ')'); //获得jsonObj对象
   alert(jsonObj.label);
   alert(jsonObj.value);
   var arrObj = eval(jsonObj.value);//然后就可以对arrObj做数组运算了
   alert(arrObj );

}

结果:
data

获取到'{"label":"1","value":[{"id":1,"pid":null,"name":"娱乐"},{"id":101,"pid":1,"name":"音乐"},{"id":112,"pid":1,"name":"卡通"}],"text":""}';
json.label获取到1;
json.value获取到[{"id":1,"pid":null,"name":"娱乐"},{"id":101,"pid":1,"name":"音乐"},{"id":112,"pid":1,"name":"卡通"}]数组;
arrObj 获取到":[{"id":1,"pid":null,"name":"娱乐"},{"id":101,"pid":1,"name":"音乐"},{"id":112,"pid":1,"name":"卡通"}]