开发者

jquery ajax not rendering html

I'm returning some preformtatted data from the server as a string. this is passed to the ajax results. For some reason the html is just displayed as a string rather than links etc.

this is what I want

<a href='#'>test</a>

this is what i get

&lt;a href='#'&gt;test&lt;/a&gt;

This is my code

    function displayData(startRow, pageNumber) {
        // there's a call to jQuery AJAX here 
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "default.aspx/GetPageLinks",
            data: "{startRow: '" + startRow + "', pageNumber: '" + pageNumber + "'}",
            dataType: "json",
            success: function(msg) {
                displayRoutes(msg.d);
            }
        });
        return false;
    }
    });

    function displayRoutes(results) {
        $('#tradeRoutes'开发者_运维技巧).html(results);            
    }


Are you 100% sure you didn't already pass the response to the POST through an encoding stage at the server side?

Update: i.e. is the server returning a string with real angle brackets in, or is it returning a string full of &lt;/&gt; encoded angle brackets?

If it's the latter, then the problem is at the server, not at the browser, because you want to be sending real HTML, not HTML encoded as text.

I'd use something like Fiddler (or Wireshark, or Firebug) to check what the actual server response is.

Also put an alert() call in displayRoutes to see what 'results' is actually set to.


Building on Will Dean's answer, try this:

function decodeHtml(encodedHtml) {
    return encodedHtml.replace(/&amp;/g,'&')
        .replace(/&lt;/g,'<').replace(/&gt;/g,'>');
}

function displayRoutes(results) {
    $('#tradeRoutes').html(decodeHtml(results));
}

This is from the jQuery plugin equivalent here: jQuery plugin - HTML decode and encode

At least this should get your code working while you figure out what's making it do it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜