Java中如何获得servlet生成的验证码通过Ajax显示到界面
servlet图片生成没问题,Ajax也能访问到servlet,状态码是200,就是不知道怎么将servlet生成的验证图片显示到界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>ajax</title>
<script type="text/java script" src="/changeImg.js"></script>
<script type="text/javascript">
var req;
function changImg() {
var url = "/changeImg/ImageServlet";
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET",url, true);
req.onreadystatechange = complete;
req.send(null);
}
}
function complete(){
if (req.readyState == 4) {
if(req.status==200){
这段代码怎么修改才能将servlet里的验证码图片显示出来
document.getElementById(‘imgObj’).innerHTML=req.responseText;
}
}
}
</script>
<head>
<body>
这里页面第一次加载的时候图片可以看得到,但通过Ajax就获取不到了
<img id="imgObj" alt="" src="ImageServlet" />
<a href="javascript:changImg()">看不清,换一张</a>
</body>
</html>
------解决方案--------------------
<img id="imgObj" alt="" src="ImageServlet" />
<a href="#" onclick="changImg();return false">看不清,换一张</a>
function changImg(){
document.getElementById("imgObj").src="ImageServlet?" + Date.parse(new Date())
}