I need to submit a form and show the corresponding message (submitted / something went wrong) using ajax. I have a page with a form to send to the server, I use javascript / ajax to send requests to the server. How to get answers from the server and show them on the page?
I have currently written "xmlhttp.responseText", but what about if I need to show server responses?
I also used the following to show the message, but it only works when I submit a request using my form, not javascript.
Any other way to show the meaning of variables? after server response to ajax request?
property
<s:property value="message"/>
Javascript function
function AddToWatchList(value){ if(window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("message").innerHTML = xmlhttp.responseText; } } xmlhttp.open("get","add?myvalue="+value,false); xmlhttp.send(); }
Server code
... private String message; public String add(){ .... this.message = "added"; return "success"; } getter and setter of message .....
xmlhttp.responseText is not the right answer, since I want to show the value of a specific variable, let's say that there is a message variable on the server.
The world of Moordio has provided an excellent solution, but is there a better way to do this?
source share