前端JSON.parse: Bad control character in string literal in JSON

JSON.parse: Bad control character in string literal in JSON

# Don't listen to some people's blind BB, it's not caused by the large JSON data at all.

Phenomenon:

The backend returns JSON data to the frontend, and the frontend JS converts JSON, for example:
jQuery.parseJSON(str.replace(/\r|\n/ig,""));
JSON.parse(str)
will report: Bad control character in string literal in JSON

reason:

It is due to a problem with the format of json, which may be the following:
1. There are /n line breaks in the original data (this is the most common reason)
2. There are spaces before and after the data

solution:

1. http://jsonlint.com to check whether the json format is correct, and deal with the wrong format
insert image description here
in turn. 2. Remove spaces and line breaks at the front end before submitting
. 3. If you need to echo the data, you can remove spaces/line breaks in the background code

Guess you like

Origin blog.csdn.net/qq_16843563/article/details/127019628