Reputation: 375
I have an jsp page, index, this is it's code:
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="alteraSala.js"></script>
<div id="conteudo">
<select id="sala">
...
</select>
<div id="resultado"></div>
</div>
My servlet is something like:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/hello")
public class Hello extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println("<b>HELLO</b>");
}
}
I've also got an AJAX file:
$(document).ready(function () {
$('#sala').change(function (event) {
var name = $('#sala').val();
$.get('../EditarSala', {
sala: name
}, function (responseText) {
$('#resultado').text(responseText);
});
});
});
The problem is that the output of the page is not: HELLO It's: <b>HELLO</b>
Changed:
response.setContentType("text/plain");
to:
response.setContentType("text/html");
Still isn't working
For some reason when I change the AJAX file from:
$('#resultado').text(responseText);
to:
$('#resultado').append(responseText);
It displays the text properly, but I can't use append.
Upvotes: 1
Views: 5543
Reputation: 6994
I think you have to use either $.ajax()
or $.post()
because you are also sending data.
Also your mistake is use html()
not text()
method of jquery.Because The html()
method sets or returns the content (innerHTML) of the selected elements.But The text()
method sets or returns the only text content of the selected elements.
$(document).ready(function () {
$('#sala').change(function (event) {
var name = $('#sala').val();
$.post('../EditarSala', {
sala: name
}, function (responseText) {
$('#resultado').html(responseText);
});
});
});
Upvotes: 2
Reputation: 2405
Change response.setContentType("text/plain");
to response.setContentType("text/html");
Upvotes: 1