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

Similar Posts: