0

I have the below call AJAX code in my JavaScript code:

$.get("MyServlet",function(data, status) {          
        $('#ajaxResponse').text(data);
});

MyServlet returns a piece of HTML code.

The content of my element #ajaxResponse is set as text and not as HTML code. When I inspect element I found the content of my div like this:

<table border="1">
<tr>
    <td>ID</td>
    <td>Name</td>
    <td>Surname</td>
</tr>
...
0

2 Answers 2

1

Use the jQuery .html() method:

$.get("MyServlet",function(data, status) {          
    $('#ajaxResponse').html(data);
});

The jQuery .text() method escapes all HTML found in the data.

Reference: https://api.jquery.com/html/

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

1 Comment

yes :) That is :) thanks a lot
1

Use $().html() instead.

$().text() escapes every HTML tag.

From the docs:

We need to be aware that this method escapes the string provided as necessary so that it will render correctly in HTML. To do so, it calls the DOM method .createTextNode(), does not interpret the string as HTML.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.