[development experience] solution to unexpected syntax error: unexpected identifier in JSON parsing

The solution of unexpected syntax error: unexpected identifier

Analysis of the causes of the solutions to errors

Error phenomenon

As shown in the title, this error occurs when the foreground parses the JSON object passed in the background. Other code is very simple, as follows:

$.getJSON("Index_ToDoList", { type: "GetBacklog" }, function (data) {
                   var rowData = eval('(' + data + ')');
                   alert(rowData[0].RiskNumber);
               }
        })

Results no matter how to test and modify, always report the above error. Online Baidu also many times, are saying less or more a “comma”, but tested the background, JSON format is no problem.

resolvent

One hour later, when I couldn’t figure out the solution, I suddenly found that in other code, the parsing was done by another method. I quickly tested the code as follows:

    $.ajax({
               url: 'Index_ToDoList?random=' + Math.random(),
               data: { "type": "GetBacklog" },
               success: function (data) {
                   var rowData = eval('(' + data + ')');
                   alert(rowData[0].RiskNumber);
               }
        })

It’s OK! The data is also parsed out!

Cause analysis

The most likely reason is that there is a problem with the version of jQuery quoted in this framework, or some code has been changed by others (I don’t have the ability to look up the code), so this writing method is feasible in this framework. It can be seen that maintaining the code left behind by others is such a pitfall, but sometimes, if you can’t rewrite the code, you have to be more careful!


Read More: