如何从 div 内的 servlet 获取响应数据?
我有这段简单的 html 代码,每当我单击任何按钮时,服务器都会发回一些字符串,我希望它显示在 #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>
在JS中,我使用基于浏览器的XMLHttpRequest
或ActiveXObject
向servlet发送请求,我可以在client.responseText()
中获取响应那么我如何将此输出定向到我的 #response
div ?
这是我的 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 方法
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");
}
但是使用 js 脚本而不是 jquery,我无法从 servlet 获取输出
感谢大家提供 jQuery 代码,Javascript 中还有其他替代方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需使用简单的java脚本即可
:
document.getElementById("response").innerHTML=res;
simply do
in simple java script:
document.getElementById("response").innerHTML=res;
如果您在项目中使用 jQuery:
$('#response').load("http://url...")
http://api.jquery.com/load/
If you're using jQuery in your project:
$('#response').load("http://url...")
http://api.jquery.com/load/
由于您使用的是 jQuery(基于问题标签的假设),因此您可以使用
.load()-method
:
这样做的好处是 jQuery 可以为您完成所有 ajax 工作(创建和处理 XMLHttpRequest 和 ActiveX 内容)。
Since you are using jQuery (assumtion based on the question tags), you can use the
.load()
-method:This has the benefit of jQuery doing all of the ajax work for you (creating and handling XMLHttpRequest and ActiveX stuff).