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>
...
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
senior
  • 2,196
  • 6
  • 36
  • 54

2 Answers2

2

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.

Phiter
  • 14,570
  • 14
  • 50
  • 84
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/

Anne Douwe
  • 681
  • 1
  • 5
  • 19