jQuery VM603:1 未捕获的语法错误:JSON 中的意外标记 o 位于位置 1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37914180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:08:48  来源:igfitidea点击:

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

jqueryjsonajax

提问by Joe.Herylee

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?

我想将 JSON 对象解析为数组,并将数组呈现到 HTML 中。但我不知道如何通过以下方式处理它?

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.

下面是我的部分js代码,showTable(data)函数是将json对象解析成html table。函数requestData()是后面的请求信息。

 (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);
        }       
    })();

回答by Danielle Davis

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

我有同样的问题,如果你的响应头是 application/json,它已经被解析,你不需要解析它:

var dataArray = $.parseJSON(data);

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

此外,您可以通过以下方式获取它jQuery.getJSON()jQuery.getJSON()

回答by Stéphane Molano

In your ajax request you should specify the dataType as "html". Among others : when data is generated by php json_encode() function.

在您的 ajax 请求中,您应该将 dataType 指定为“html”。其中:当数据由 php json_encode() 函数生成时。

 $.ajax({
            type : "GET",
            url : "data/data.json",
            dataType : "html",
            data : data,                    
            success:function(msg){
                console.log(msg);
                var array_return =  $.parseJSON ( msg );
            },
            error:function(msg){
                console.log("failed");
            }
        }); 

回答by Waheed

So here is the trick with JSON parser

所以这是使用 JSON 解析器的技巧

JSON.parse function which is mostly used in $.parseJSON(data) from jquery. Will always assume a string starting with ' a single quote.

JSON.parse 函数,主要用于 jquery 的 $.parseJSON(data) 中。将始终假定以 ' 开头的字符串是单引号。

So if your data looks like this

所以如果你的数据看起来像这样

let ok = '{"hello": "world"}'; //success
let fail = "{'hello': 'world'}"; //fail

and then

进而

JSON.parse(ok);
JSON.parse(fail);

With a single quote it will work just as expected, otherwise it will fail with Unexpected token o in JSON at position 1?and the reason is Single quotes (') are not allowed in JSON as safari explains.

使用单引号它将按预期工作,否则它将失败,Unexpected token o in JSON at position 1?原因是 safari 解释说,JSON 中不允许使用单引号 (')。

Hope that helps :)

希望有帮助:)