首頁 >後端開發 >php教程 > 求解php传数组js解用.该怎么解决

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

WBOY
WBOY原創
2016-06-13 13:42:01828瀏覽

求解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 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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn