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

前端 未结 6 906
情歌与酒
情歌与酒 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 14:03

    When creating JSON with HTML you have to escape then double-quote character, and also the CLRF character and it will return HTML fine.

    I worked with a pl/sql in oracle procedure that was called from a AJAX request. If you would like, i can post the implementation.

    You should test your output at http://www.jsonlint.com/ to see if its valid.

    So

    {
        "id": "1",
        "html_value": "test link returnin html code"
    }
    

    is diffrent from

    {
        "id": "1",
        "html_value": "test link returnin
     html code"
    }
    

    because of the CLRF character. You should replace that in the server side with  ; or
    .

    Hope this helps, Alex

提交回复
热议问题