ajax与servlet的数据传送问题,servlet如何向ajax传数据,ajax如何接收servlet传来的数据
这是我试着写的,但点击button没有反应,是什么原因<html><head><metacharset="UTF-8"/></head><scripttype="text...
这是我试着写的,但点击button没有反应,是什么原因
<html>
<head>
<meta charset="UTF-8"/>
</head>
<script type="text/javascript">
function change(){
var xml;
if(window.XMLHttpRequest)xml=new XMlHttpRequest();
else xml=new ActiveXObject("Microsoft.XML HTTP");
xml.onreadystatechange=function(){
if(xml.readyState==4&&xml.status==200){
var xmlDoc=xml.responseText();
docuement.getElementById("div").innerHTML=xmlDoc;
}
xml.open("GET","D:\Pokemon\src\Info\JDBC.java",true);
xml.send();
}
</script>
<body>
<div id="div">I</div>
<input type="button" onclick="change();" value="按钮"/>
</body>
</html>
public class JDBC extends HttpServlet {
private static final long serialVersionUID = 1L;
public JDBC() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
JDBCInterface jdbc=JDBCInterface.getJDBC("data");
String[] val=jdbc.getData("form");
String msg="";
for(int i=0;i<val.length;i++){
if(val[i]==null)msg+="null;";
msg+=val[i];
}
response.getWriter().println(msg);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
} 展开
<html>
<head>
<meta charset="UTF-8"/>
</head>
<script type="text/javascript">
function change(){
var xml;
if(window.XMLHttpRequest)xml=new XMlHttpRequest();
else xml=new ActiveXObject("Microsoft.XML HTTP");
xml.onreadystatechange=function(){
if(xml.readyState==4&&xml.status==200){
var xmlDoc=xml.responseText();
docuement.getElementById("div").innerHTML=xmlDoc;
}
xml.open("GET","D:\Pokemon\src\Info\JDBC.java",true);
xml.send();
}
</script>
<body>
<div id="div">I</div>
<input type="button" onclick="change();" value="按钮"/>
</body>
</html>
public class JDBC extends HttpServlet {
private static final long serialVersionUID = 1L;
public JDBC() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
JDBCInterface jdbc=JDBCInterface.getJDBC("data");
String[] val=jdbc.getData("form");
String msg="";
for(int i=0;i<val.length;i++){
if(val[i]==null)msg+="null;";
msg+=val[i];
}
response.getWriter().println(msg);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
} 展开
展开全部
你这个java代码我就不改了,改一下你的前端代码,原生ajax代码很繁杂,jquery就简单多了,我写完了你就能看懂
//首先引入js,本地文件获取cdn都行,我用cdn
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<div id="div1"></div>
<input type="button" onclick="change();" value="按钮"/>
function onchange(){
$.ajax({
url:"JDBC",
type:"get",
success:function(msg){
//msg就是你得到的后台返回值
$("#div1").text(msg);
}
});
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询