Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VM603:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1?

Tags:

json

jquery

ajax

n.parseJSON @   jquery-1.12.3.min.js:4
showTable   @   query.html:107
success @   query.html:98
i   @   jquery-1.12.3.min.js:2
fireWith    @   jquery-1.12.3.min.js:2
y   @   jquery-1.12.3.min.js:4
c   @   jquery-1.12.3.min.js:4

Above is the error info of code.

I want to parse JSON object to array,and render the array into the HTML. But I don't know how to handle it by the following?

Following is the part js code of mine , function of showTable(data) is parse the json object into html table.function requestData() is request info from the back.

 (function(){
        var form = $("form");
        var contentCenter = $(".content-center");
        $(".s-btn").on("click",function(event){
            $(".container").css({
                "position":"relative"
            })
            form.css({
                "position":"absolute",
                "left":"15px",
                "top":"0px"                 
            });
            contentCenter.css({
                "position":"absolute",
                "top":"-12px"   
            })

            event.preventDefault();
            requestData();
        });

        //与后台交互数据
        function requestData(){                                 

            var data = {
                type : $("#form_control").val(),
                keywords : $.trim($("#ipt_control").val())
            };
            $.ajax({
                type : "GET",
                url : "data/data.json",
                dataType : "json",
                data : data,                    
                success:function(msg){
                    //TODO请求成功后跳转页面
                    //处理后台返回的数据
                    console.log(msg);
                    showTable(msg);
                },
                error:function(msg){
                    console.log("failed");
                }
            }); 
        }   
        //获取json数据,动态创建表格
        function showTable(data){
            var dataArray = $.parseJSON(data);
            console.log(dataArray);
            var tableStr="<table class='table table-bordered'>"
            tableStr = tableStr + "<thead><td>id</td><td>name</td>handle<td></td>"
            var len = dataArray.length;
            for(var i=0;i<len;i++){
                tableStr = tableStr + "<tr><td>" + dataArray[i].id + "</td>" + "<td>" + dataArray[i].name + "</td>" + "<td>" + dataArray[i].handle + "</td></tr>";
            }
            tableStr = tableStr + "</table>"
            $("#dataType").html(tableStr);
        }       
    })();
like image 400
Joe.Herylee Avatar asked Jun 20 '16 03:06

Joe.Herylee


1 Answers

I had this same problem, if your response header is application/json, it is already parsed, you don't need to parse it with:

var dataArray = $.parseJSON(data);

Also, you can get it with jQuery.getJSON() : jQuery.getJSON()

like image 79
Danielle Davis Avatar answered Sep 18 '22 12:09

Danielle Davis