当前位置: 代码迷 >> PHP >> 求解php传数组js解用.该怎么解决
  详细解决方案

求解php传数组js解用.该怎么解决

热度:136   发布时间:2012-03-29 12:53:12.0
求解php传数组js解用....
js异步请求php文件返回数组echo js_encode($list);
数组输出为:function(data){alert(data)}
{"1":{"id":54,"neme":"sdfa","0":"pic","1":"http:\/\/localhost\/xin\/index.php?app=home&mod=new&act=newlist"},"2":{"id":46,"neme":"sdfa","0":"pic","1":"http:\/\/localhost\/xin\/index.php?app=home&mod=welcome&act=newlist"},"3":{"id":56,"neme":"\u5212","0":"pic","1":"http:\/\/localhost\/xin\/index.php?app=home&mod=welcome&act=\u611f\u5230"},"4":{"id":58,"neme":"sdfa","0":"pic","1":"http:\/\/localhost\/xin\/index.php?app=space&mod=welcome&act=\u548c"},"5":{"id":87,"neme":"\u7ba1\u7406\u5458","0":"pic","1":"http:\/\/localhost\/xin\/index.php?app=home&mod=come&act=newlist"},"6":{"id":54,"neme":"sdfa","0":"pic","1":"http:\/\/localhost\/xin\/index.php?app=home&mod=welcome&act=\u8c01\u7684"}}
但for循环输出时却发现被单解了....代码如下.

function(data){
alert(data);
  alert(data.length);//弹出为692
for(var i=0;i<data.length;i++){
alert(data[i]);//弹出为未定义
}

但是我把弹出的数组复制符值却可以..
如下:
var data1={"1":{"id":54,"neme":"sdfa","0":"pic","1":"http:\/\/localhost\/xin\/index.php?app=home&mod=new&act=newlist"},"2":{"id":46,"neme":"sdfa","0":"pic","1":"http:\/\/localhost\/xin\/index.php?app=home&mod=welcome&act=newlist"},"3":{"id":56,"neme":"\u5212","0":"pic","1":"http:\/\/localhost\/xin\/index.php?app=home&mod=welcome&act=\u611f\u5230"},"4":{"id":58,"neme":"sdfa","0":"pic","1":"http:\/\/localhost\/xin\/index.php?app=space&mod=welcome&act=\u548c"},"5":{"id":87,"neme":"\u7ba1\u7406\u5458","0":"pic","1":"http:\/\/localhost\/xin\/index.php?app=home&mod=come&act=newlist"},"6":{"id":54,"neme":"sdfa","0":"pic","1":"http:\/\/localhost\/xin\/index.php?app=home&mod=welcome&act=\u8c01\u7684"}}
..........................................................
这是怎么回事?

------解决方案--------------------
php 的 echo js_encode($list);
在 js 中得到的是字符串,你需要将其转换成 js 的对象。
最简答的方法就是 eval
  相关解决方案