I have this simple html piece of code, whenever I click on any of the buttons the server will send back some string, I want it to be displayed in #response
div.
<form target="_self" method="post">
<table>
<tr>
<td width="35"><input type="button" id="btncreate" value="create" onclick="getData();"></td>
<td width="35"><input type="button" id="btnupdate" value="update"></td>
<td width="30"><input type="button" id="btndelete" value="delete"></td>
</tr>
<tr>
<td>Name : </td>
<td><input type="text" id="name" value=""></td>
</tr>
<tr>
<td>File : </td>
<td><input type="file" id="filname" value=""></td>
</tr>
</table>
</form>
<div id="response"></div>
In JS i am using XMLHttpRequest
or ActiveXObject
based on browser to send request to servlet, and i can get the response in client.responseText()
so how can i direct this output to my #response
div?
Here is my JS
function getData()
{
var client;
var data;
var url_action="/temp/getData";
if(window.XMLHttpRequest)
{
client=new XMLHttpRequest();
}
else
{
client=new ActiveXObject("Microsoft.XMLHTTP");
}
client.onreadystatechange=function()
{
if (client.readyState==4 && client.status==200)
{
document.getElementById("response").innerHTML=client.responseText;
}
};
data="name="+document.getElementById("name").value+"&file="+document.getElementById("filname").value;
client.open("POST",url_action,true);
client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
client.send(data);
}
Servlet post method
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
log.info("Good");
out.println("Good to go");
}
But using js script not jquery, i am not able to get the output from servlet
Thanks all for a jQuery code, any alternative in Javascript?