Ciao,
sto implementando un'applicazione web che da una pagina html vada ad interrogare una servlet, passando da javascript e AJAX.
questo è l'html
codice:
<html>
<head>
<script type="text/javascript" src="funzioni.js">
</script>
<title>Esempio di Servlet</title>
</head>
<body>
<h1>Quotazioni</h1>
Testa
<div id="wallet"></div>
</body>
</html>
questo il javascript funzioni.js
codice:
function test()
{
var ajax = new XMLHttpRequest();
ajax.open("GET", "http://localhost:8080/servlet_esempio/servlet/Funzioni", true);
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1");
ajax.send(null);
var output=ajax.responseText;
document.getElementById("wallet").innerHTML = "L'output è " + output;
}
ed infine la Servlet Funzioni.java
codice:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class Funzioni extends HttpServlet {
private static final long serialVersionUID = 1L;
public Funzioni() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("TESTO DI ESEMPIO");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
in pratica se uso Chrome, la readyState dell'oggetto XMLHTTPRequest rimane fisso a 1 non riuscendo quindi ad accedere al campo responseText (la risposta della mia Servlet)
Dove sbaglio?