console.log prompts like this:
data: '{"site_name":"aaaa","site_keywords":"bbbb","site_beian":"闽ICP备8888888888号","site_description":"ccccc","site_statistic":"<a>wsdfadfasdfasdfasdfasdf</a>"}',
Obtaining json data from mysql, this problem occurs. How does the front end handle the conversion and parsing of the json type?
淡淡烟草味2017-05-18 10:49:53
Convert String to JSON object :
1 var res = JSON.parse(data);
or
2 var res = data.parseJSON();
or
3 var res = eval('('+ data +')');
我想大声告诉你2017-05-18 10:49:53
Try to use JSON.parse, eval is not recommended;
Try to add try...catch, the probability of errors when converting JSON to objects is quite high, such as unescaped characters and too many nesting levels;
try{
var myObject = JSON.parse(data);
} catch(e){
console.log(e);
}
phpcn_u15822017-05-18 10:49:53
The backend sets the Content-Type of the response header to application/json, and the data outputs a string in json format. The frontend automatically obtains the JSON object, which can be processed as an ordinary js object.
ringa_lee2017-05-18 10:49:53
var data=[{"site_name":"aaaa","site_keywords":"bbbb","site_beian":"闽ICP备8888888888号","site_description":"ccccc","site_statistic":"<a>wsdfadfasdfasdfasdfasdf</a>"}];
console.log(JSON.stringify(data));