Home > Backend Development > PHP Tutorial > 求解php传数组js解用.该怎么解决

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

WBOY
Release: 2016-06-13 10:12:49
Original
783 people have browsed it

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template