0

After a successful ajax call, I want to print the result on screen (in a DIV). This result is a piece of HTML code.

$.ajax({
    url: window.location.pathname + '/feedback-campaigns',
    type: 'GET',
    contentType: 'text/html',
    success: function(data, textStatus, jqXHR) {
        var result = data.campaigns.campaigns[0].text; 
        // result = "<b>TEST - do you see me?</b>"
        $('.campaign-top-product-detail-page').html(result);
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log('NOK');
    }
});

But, on screen I see this:

<b>TEST - do you see me?</b>

instead of the text in bold.

3 Answers 3

3

Use following

$.ajax({
           url: window.location.protocol + '//' + window.location.host + window.location.pathname + '/feedback-campaigns',
           type: 'GET',
           contentType: 'text/html',
           success: function(data, textStatus, jqXHR) {
                    var result = $.parseHTML(data.campaigns.campaigns[0].text); // result = "&lt;b&gt;TEST - do you see me?&lt;/b&gt;"
                    $('.campaign-top-product-detail-page').html(result);  
          },
          error: function(jqXHR, textStatus, errorThrown) {
                    console.log('NOK');
          }
}); 

Update: write like this $.parseHTML(data.campaigns.campaigns[0].text);

Sign up to request clarification or add additional context in comments.

3 Comments

Uncaught ReferenceError: parseHTML is not defined
Make it correct----- $.parseHTML(data.campaigns.campaigns[0].text)
It works, thanks! However, I had to add something: var result2 = $.parseHTML(data.campaigns.campaigns[0].text)[0].data;
3

It is because the response from server contains HTML entities, not actual HTML tags. To solve this, simply send the HTML such as <b>TEST - do you see me?</b>. (If you're using PHP on your server, then don't use htmlspecialchars on the output).

Comments

0

Remember that things like &lt will be not regarded as a part of HTML code but some character. So if you want to show the text which is bold, you should let it return as below

<b>TEST - do you see me?</b>

However, if you don't have permission to edit the result, other answers are good.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.