简体   繁体   中英

Uncaught SyntaxError: Unexpected token \

I have a stringified JSON-Object inside another JSON-Object. I get it via $.ajax() from my database like this:

data => {"foo":"[{\\"foo\\":\\"bar\\"},{\\"foo2\\":\\"bar2\\"}]"}

When trying to do JSON.parse(data.foo) , I get an Uncaught SyntaxError: Unexpected token \\ .

What am I doing wrong? (Apart from the fact, that the object needs to be saved as a string.)

You have to escape html tags. Which language are you using in back end? In python Django it is done by using the safe filter. Similar might exist in php or other languages.

Thank you for your help. It seems, the data coming from the database "looked" right, but wasn't. I assume this was because I put the data in the database by hand. Now I put the data via my API with JSON.stringify() and now everythings looks fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM