views:

54

answers:

3

Using an .net MVC2 app is generating a string of HTML code that be copy/pasted by my users to a 3rd party's web app. In other words, i'm not looking to render the HTML - i want the string to be displayed verbatim.

I need to understand what JSON/jQuery properties have to be explicitly set so that my app's string shows up as pure ascii - the HTML codes are displayed unaltered, unescaped, unrendered.

tx

+2  A: 

JSON itself is just text (ASCII, UTF-8, or otherwise). HTML embedded within it has absolutely no special meaning.

The only reason the browser would ever render that text as HTML is if you inject it onto the page as such, perhaps using the .html() function.

If you want to inject it as text, try the .text() function or, if you're talking about a textarea or other form element, .val().

You don't need to make any changes to the ASP code or JSON; it's purely a question of what you do with the content once you've got it.

VoteyDisciple
A: 

Just generate the string of html using html special characters. That is, turn all of the less than to < and so on. I don't know asp, but I am sure it has some htmlspecialchars() (php) equivalent. This has nothing to do with JSON or jQuery. Once you have the properly updated string, then you don't have anything to worry about.

If you get the text as pure html with Javascript (hard coded, Ajax, etc.) you can just update the container with $("#container").text(html); This will turn it into readable html instead of interpreting the html itself (essentially doing what htmlspecialchars() does)!

tandu
+1  A: 

You just need to take your code and run it through HtmlEncode

<%= Server.HtmlEncode(YourHtmlHere) %>
Chao