日期:2014-05-16  浏览次数:21106 次

ajax返回一个json数据,前端js解析问题
{totalCount:2,success:true,error:"",singleInfo:"",data:[{"ID":"2","UserName":"123","Name":"111","ViewCount":"0","Images":"Upload/201404/25/20140425135607135672.jpg","Age":"7"},{"ID":"3","UserName":"12112","Name":"111","ViewCount":"1","Images":"Upload/201404/25/20140425135607985594.jpg","Age":"8"}]}


返回数据如上,可以获取到返回数据,js肿么解析出data里的数据,循环添加到
<ul id="all"><li></li><ul>
 ul里,

<script>
                function Search() {
                    $.ajax({
                        type: "POST",
                        url: "Search.aspx?sname=111",
                        dataType: "html",
                        //data: "sname=111",
                        success: function (msg) {
                            var jsonText = JSON.stringify(msg);
                            alert(jsonText);
                        },
                        error: function () {
                            alert("error");
                        }
                    });
                }
        </script>


第一次用json不熟悉,求教各位
------解决方案--------------------
jsonTest.data;改在jsonText.data
------解决方案--------------------
 dataType: "html",

这样msg就是string类型的,要eval转为json对象,你返回的字符串不是标准json格式的字符,不能指定dataType为JSON,要不success都不执行


function Search() {
    $.ajax({
        type: "POST",
        url: "Search.aspx?sname=111",
        dataType: "html",
        //data: "sname=111",
        success: function (msg) {
  &