In jQuery, I am returning HTML in a JSON result, what do I have to escape?

前端 未结 6 913
情歌与酒
情歌与酒 2020-12-15 13:07

In my Ajax request (using jQuery) I am returning a JSON response.

So json.Html will have a string of HTML I want to append inside a div.

On the server side,

6条回答
  •  难免孤独
    2020-12-15 13:45

    You only need to add slashes to quotes and slashes (ala \" and \\) when escaping content to be put into a JSON string. HTML characters mean nothing inside a JSON string, so they're fine :)

    Of course, make sure your string is itself well-formed (X)HTML so that it doesn't explode when inserted into the div.

提交回复
热议问题