Load HTML into received DIV via AJAX request by converting special characters

Asked

Viewed 366 times

2

I am maintaining an application where there is now the need to load an HTML from the database into a div. Regarding the return of this database HTML, it’s cool, I already receive the values in a JSON and could (eventually) load it in the view, but simply does not load.

I’ll show you the code:

$.ajax({
    url: '<minha url>',
    dataType: 'html',
    type: 'post', 
    data: {<meus parametros>},
    success: function(data){
        $('#load-html-template').html(data[0].TemplateText);
    }

As to the dataType, have already sent as HTML (returns nothing), JSON (returns HTML as a string), XML (loads nothing), etc...

Solved one part of the problem - entering another

Thanks for your help so far, but now I have direct access to the database and realized that, the HTML code is in special characters format, this way:

&lt;html&gt;
&lt;head&gt;

&lt;/head&gt;

&lt;body&gt;
    &lt;table border="0" cellspacing="0" cellpadding="0" width="100%"&gt;
        &lt;tr&gt;
            &lt;td width="2%"&gt;&amp;#160;&lt;/td&gt;
            &lt;td width="920px"&gt;
                 &lt;div style="line-height:120%;font-family:Arial;font-size:15pt;background-color:#e8eae8;"&gt;&lt;form action=""&gt;&lt;table border="0" cellspacing="1" cellpadding="0" width="99%"&gt;

......

Well, how to convert these tags to the HTML tags in question? I’ve done it, but still the problem follows...

mystring.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;");

Could give me a strength!!! Thanks in advance!!!

  • You can put here what appears with console.log(data);?

1 answer

1


I tried the following:

var html = String(data[0].TemplateText).replace(/&amp;/g, '&')
        .replace(/&quot;/g, '"')
        .replace(/&#39;/g, "'")
        .replace(/&lt;/g, '<')
        .replace(/&gt;/g, '>');
  • It worked... Thanks!!!

  • 1

    There is no need to use double quotes to assign single quotes to a string. Simply assign with exhaust: '\''.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.