Tag Archives: Unexpected token o in JSON at position 1

When parsing background JSON data with Ajax: unexpected token o in JSON at position 1

JSON data parsing exception

When doing JSON data today, the following error occurred, saying that it was an exception to parse

VM1584:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at Object.success (customer.js:170)
    at j (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at x (jquery.min.js:4)
    at XMLHttpRequest.<anonymous> (jquery.min.js:4)

The request function is as follows:

$.ajax({
        url: "../../XXX.php",
        data: {CustomerName: $("#CustomerName").val()},
        dataType: "json",
        type: "post",
        success: function(data) {
            var jsonData = JSON.parse(data);
            alert(data[0].code);
            alert(data[0].msg);
        },
        error: function() {
            alert("error!");
        },
    });

It should be noted that when using jQuery to complete Ajax requests, there is a . between Ajax and $. You can’t leave it out here. Parsing exception is because after the end of Ajax request, the JSON data transferred in the background has been automatically converted to object type. Therefore, there is no need to use JSON. Parse manual conversion here

How to Solve Error: Unexpected token o in JSON at position 1

Unexpected token o in JSON at position 1

1、 Summary

In a word:

solution: remove the layer transformation of JSON. Parse()
error reason: because the data you want to convert is object, the JSON. Parse() method parses a string into a JSON object, and you will report an error if you convert it again

1. Cause of error: question in data of Vue_ list_ box:JSON.parse (‘{!! json_ encode($question_ list_ box !!}’), How to solve it

can use JSON in PHP_ After encode(), it is directly given to the window object of the page, which will be directly converted into a JSON object. In this way, you don’t need to use the JSON. Parse() function

2. What is the function of JSON. Parse()

JSON. Parse() is used to parse a JSON object from a string

2、 Unexpected token o in JSON at position 1

Refer to: unexpected token o in JSON at position 1 https://blog.csdn.net/wxl1555/article/details/79184076

 

Words written in the front

This problem was encountered once before when doing a project. At that time, according to the practice on the Internet, after removing the layer of JSON. Parse (), there was no such error report, and the data could be used normally. I didn’t think much about it, and I didn’t go deep into the reason

But this time we met again, so we have to find out the reason this time

Let’s see what it does first

JSON. Parse () is used to parse a JSON object from a string, such as

var str = '{"name":"LeonWu","age":"18"}'

JSON.parse(str);

// age: "18";
// name: "LeonWu";

JSON. Stringify () is used to parse a string from an object, such as

var a = {a:1,b:2,c:"LeonWu"};
 
 JSON.stringify(a);
 
 //output "{"a":1,"b":2,"c":"LeonWu"}"
 

The reason behind it

reasons for error reporting

Because the data you want to convert is originally object, JSON. Parse() is to parse a string into a JSON object, and you will report an error if you convert it again

Why is there such a mistake

When the object is passed as a parameter to JSON. Parse(), it will be converted to string by using the tostring() method, and the result is “[object]”

JSON. Parse() interprets the first character ‘[‘ as the beginning of the array, and the second character ‘o’ doesn’t know how to handle it; So the above error message unexpected token o in JSON at position 1 is thrown

 

The solution is to remove the layer transformation of JSON. Parse (), because the data you need to transform is a JSON object, so you don’t need to transform it