开发者

Json returned my HTML <br /> in unicode form \u003cbr /\u003e . Causing the <br /> printed as a text rather than making a new line

开发者 https://www.devze.com 2023-02-06 10:18 出处:网络
I\'m using ASP.net MVC3 and i returned a model which is in Json format using Jquery.AJAX and then I pass it into a Jquery template to be printed out.

I'm using ASP.net MVC3 and i returned a model which is in Json format using Jquery.AJAX and then I pass it into a Jquery template to be printed out.

For example the Json that server returned is {"Key":2,"Content":"I'm Jason\u003cbr /\u003ehow are you"} instead of {"Key":2,"Content":"I'm Jason <br /> how are you"}

when I append it into an Div using Jquery template it printed out something like this:

I'm Jason <br /> how are you

while the intended r开发者_运维问答esult should be

I'm Jason 
how are you

Am I suppose to prevent the server from encoding the string in server side? But I think this may cause security issue.

Therefore I think I have to decode the Json string in client side but no luck so far. Can anyone show me an appropriate way to deal with this kind of problem?

Thanks

*Updated

I tested with jQuery('#someDiv').append(data.Content); and it print out as intended.

So the problem is probably related to Jquery template

I'm using this code to pass data into Jquery template jQuery('#someTemplate').tmpl(data).appendTo('#someDiv');

My Jquery template

<script id="someTemplate" type="text/x-jquery-tmpl">
 <div>${Content}</div>
</script>


I ran into this same issue. No need encode/decode or escape/unescape.

Instead of this:

${Content}

Use this:

{{html Content}}

The unicode with be displayed as HTML.


You should unescape your Content string, e.g.:

alert(unescape('\u003cbr /\u003e'));


I have the same issue with ajax templates. the string has a \n which gets ignored on the template and if I convert the \n to <br/> then the <br /> is displayed instead of the linebreak.

if I use {{ unscape(myString) }} then the result is still 'some text <br /> next line'

0

精彩评论

暂无评论...
验证码 换一张
取 消