ciao a tutti,
sto avendo alcune difficoltà nell'utlizzare ajax. in pratica sto facendo un semplice esercizio che consiste nel compilare due form: una form si chiama dolce, e un'altra liquore. il mio obiettivo sarebbe quello di popolare automaticamente la form liquore, dopo aver inserito il nome del dolce.
per questo motivo mi sono avvalso dell'utilizzo di una servlet, che gira in locale sul servlet container tomcat.
vi posto il codice html della pagina:
qui invece vi posto il metodo post della mia servlet.Codice PHP:<HTML>
<HEAD>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="it">
<title> scegli il dolce </title>
</HEAD>
<BODY>
<script type="text/javascript">
//--Function to get the xmlhttp object
function getHttpObject(){
var request=null;
var browser = navigator.userAgent.toUpperCase(); if(typeof(XMLHttpRequest)=="function" || typeof(XMLHttpRequest)=="object"){
request = new XMLHttpRequest();
}
else if(window.ActiveXObject && browser.indexOf("MSIE 4")<0) { if(browser.indexOf("MSIE 5")<0) {
request = new ActiveXObject("Msxml2.XMLHTTP");
}else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return request;
}
function riempiliquore(stringa) {
var req = getHttpObject();
req.onreadystatechange = function() { if(req.readyState == 4) { window.alert(req.responseXML);
else {
window.alert("ERRORE");
}
}
};
req.open("POST", "http://localhost:8080/Ajax2/ajax", true); req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
req.send("dolce="+stringa); } //end riempiliquore
function gestiscirisposta(responseXML, responseText) { // window.alert(responseText);
}
</script>
<form name="ajxfrm">
<table>
<tr>
<td>dolce</td><td><input type="text" name="dolce" value=""/></td> </tr> <tr> <td>liquore</td><td><input type="text" name="liquore" value="" onfocus="riempiliquore(ajxfrm.dolce.value);"/></td> </tr>
</table>
</form>
</BODY>
</HTML>
ma la risposta che ricevo è sempre nulla....ma perchè dannazione?codice:protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String esito=""; System.out.println("do post invocato"); String dolce = request.getParameter("dolce"); System.out.println("dolce "+dolce); if(dolce.equals("tiramisu")) { esito="caffè alla nocciola"; } else{ esito="seleziona liquore"; } System.out.println(esito); StringBuffer xmlreply = new StringBuffer(); xmlreply.append("<?xml version='1.0' encoding='ISO-8859-1'?>"); xmlreply.append("<risposta>"); xmlreply.append("<dolce>"); xmlreply.append(esito); xmlreply.append("</dolce>"); xmlreply.append("</risposta>"); response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); System.out.println(xmlreply); response.getWriter().write(xmlreply.toString()); }![]()
confido nel vostro aiuto.
grazie in anticipo e buona domenica a tutti
lellone

Rispondi quotando