duanjiuhong5843 2015-11-03 12:05
浏览 57
已采纳

ajax麻烦不解析json数据php

facing a problem while parsing see below code

$('#topselector').change(function(){
                var url = "<?php echo $this->baseUrl();?>/ajax/get-all";
                //var selecter_id = $(this).val();
                //alert(selecter_id);

                $.ajax({
                    url: url,
                    type: "POST",
                    success: function(data)
                    {
                        alert("success");
                        var obj = $.parseJSON(data);
                        console.log(obj);
                        if(obj.meta.code === '200')
                        {
                            alert("ok");
                            $.each(obj.data, function(i,v){
                                //alert(v.builder_name);
                                $("#browsers").append("<option value="+v.builder_project_id+">"+v.builder_name+"</option>");
                            });

                        }
                    },
                    error: function(error){
                           alert("Error"); 
                    }
                });
            });

getting the output till success after that alerting i.e ok.

my json response is like

{
    "meta": {
        "code": 200,
        "message": "SUCCESS"
    },
    "data": [
        {
            "s_no": 1,
            "builder_project_id": "389",
            "builder_name": "1 389 Sath Build Home",
    },
    {
            "s_no": 348,
            "builder_project_id": "4",
            "builder_name": "348 4 NA",
    }
    ]
}

I cant understand where i am doing mistake pls suggest in console getting Uncaught SyntaxError: Unexpected token

  • 写回答

2条回答 默认 最新

  • du3669 2015-11-03 12:07
    关注

    Your JSON output is invalid.

    It is putting unnecessary commas at the last element of the array.

    Observe, the removed comma after builder_name's values.

    Corrected JSON:

    $json = '{
        "meta": {
            "code": 200,
            "message": "SUCCESS"
        },
        "data": [
            {
                "s_no": 1,
                "builder_d": "389",
                "builder_name": "1 389 Sath Build Home"
        },
        {
                "s_no": 348,
                "builder_project_id": "4",
                "builder_name": "348 4 NA"
        }
        ]
    }';
    

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件
  • ¥15 逐月累计,月份不连续,补齐月份
  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题
  • ¥15 vscode下编写第三方库opencv与pcl代码时没有代码提示
  • ¥15 能够跑通不报错,如何解决?(标签-matlab)
  • ¥15 MOS在RDS较大,频率高时开关波形异常
  • ¥15 SCENIC分析报错求解答