Heim >Backend-Entwicklung >PHP-Tutorial > 求解php传数组js解用.该怎么解决

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

WBOY
WBOYOriginal
2016-06-13 13:42:01825Durchsuche

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn