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条)

报告相同问题?

悬赏问题

  • ¥15 YOLOv5在进行trainpy训练后为什么会出现这种情况(语言-python)
  • ¥15 关于远程桌面的鼠标位置转换
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?
  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!