jQuery.parseJSON throws “Invalid JSON” error due to escaped single quote in JSON

后端 未结 7 926
挽巷
挽巷 2020-11-22 14:04

I’m making requests to my server using jQuery.post() and my server is returning JSON objects (like { \"var\": \"value\", ... }). However, if any of

7条回答
  •  有刺的猬
    2020-11-22 14:36

    If you need a single quote inside of a string, since \' is undefined by the spec, use \u0027 see http://www.utf8-chartable.de/ for all of them

    edit: please excuse my misuse of the word backticks in the comments. I meant backslash. My point here is that in the event you have nested strings inside other strings, I think it can be more useful and readable to use unicode instead of lots of backslashes to escape a single quote. If you are not nested however it truly is easier to just put a plain old quote in there.

提交回复
热议问题