开发者

Get link to render as html rather than text json

I am responding to clicks on li's by using $.post to post to an action method in my MVC application.

I want to send a link back in Json.

Can I have this link render as html rather than text ? how ?

I tried this, just to test the html:

var link = "<b>Hi</b>";
var encoded = Server.HtmlEncode(link);

that came out as &lt;b&gt;Hi&lt;/b&gt;

Surely there is just a Json.encode or visual studio method I can use and I don't have to format it myself? Have googled fairly extensively and can't find anything about an Json.encode

var link = "<b>Hi</b>";
var encoded = new JavaScriptSerializer().Serialize(link);

the page rendered "\u003cb\u003eHi\u003c/b\u003e"

If I send just the link variable, i.e:

var link = "<b>Hi</b>"

<b>Hi</b> renders

This is the line which sends it back:

return Json(new {Title = pTitle, Selection = pSelection, Link = pLink}, JsonRequestBehavior.AllowGet);

Starting to get frustrated, wtf!


Silly me, I didn't post enough code where the problem was:

&开发者_JAVA百科lt;script type="text/javascript">
    function TreeView_onSelect(e) {
    ...
    $.post(url, id, function (data, textStatus) {
    ...
    $("#panel-link").text(data.Link);
    }

$("#panel-link").text(data.Link);

obv has to be

$("#panel-link").html(data.Link);


Try using JavaScriptSerializer:

var link = "<b>Hi</b>";
var encoded = new JavaScriptSerializer().Serialize(link);


Try to use javascript's decodeURI() function.

http://www.w3schools.com/jsref/jsref_decodeuri.asp

<script type="text/javascript">

var uri="mytest.asp?name=ståle&car=saab";
document.write(encodeURI(uri)+ "<br />");
document.write(decodeURI(uri));

</script>

The output of the code above will be:

mytest.asp?name=st%C3%A5le&car=saab
mytest.asp?name=ståle&car=saab


I have the same problem with you and killing me whole day, I solved this problem by using Json.NET

Sample code is :

Newtonsoft.Json.JsonConvert.SerializeObject(link);

Reference http://json.codeplex.com/documentation

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜