该验证码需要在后台去匹配验证,可以选择用JSON。
如下:
function check(){
var user = document.all.user.value;
var pass = document.all.pass.value;
var cnum=Math.ceil(Math.random()*100);
var valid = document.getElementById("check_code").value;
//$.ajax({url:"sysUser.do?method=login",cache:false,data:"user="+user+"&pass="+pass+"&date="+new Date(),success:function(state){
var param = {
user : user,
pass : pass,
valid : valid,
cnum : cnum
}
// 指定处理的路径
var http_value = jQuery.param(param);
var url = "<%=path%>/broadAction/login.do?radom="+cnum;
// 获得返回值
$.getJSON(url, http_value, function(josn){
if(josn.status=="2"){
alert("验证码错误,请重新输入");
return false;
}
else if(josn.status=="1"){
window.location="<%=path%>/securityAction/showNavigation.do?radom="+cnum+"";
}else{
alert("帐户信息不正确");
//prompt("帐户信息不正确");
//window.setTimeout("document.getElementById(\"prompt\").innerHTML=''",3000);
return false;
}
});
}
// 刷新验证码图片
function refresh(obj){
[color=red]var d = new Date(); //若是IE8有缓存,需要加上序列号避免缓存
obj.src="check?t="+d.toString(38);[/color]
}
验证码TR : <tr>
<td height="30" align="right" nowrap="nowrap" class="copy1">验证码: </td>
<td><input id="check_code" name="valid" type="text" size="6">
<a οnclick="refresh(imcheck);"><img id="imcheck" src="check"></a></td>
</tr>
后台Action
private JSONObject json;//JSON
json = new JSONObject();
if(check_code.equals(valid)){ //判断验证码是否正确
}else{
json.put("status", "2"); // 验证码错误,
JSonEx.responseJson(response, json);
}
JsonEx封装类:
public class JSonEx {
private static final Log logger = LogFactory.getLog(JSonEx.class);
/**
* 方法名称:responseJson
* 创建日期:Jul 4, 2009
* 方法描述:
* @param response
* @param jsonObject
* @throws IOException void
*/
public static void responseJson(HttpServletResponse response,
JSONObject jsonObject) throws IOException {
if (jsonObject == null || response == null)
return;
String jsonTxt = jsonObject.toString();
responseJson(response, jsonTxt);
}
/**
*
* 方法名称:responseJson
* 创建日期:Dec 11, 2008
* 方法描述:
* @param response
* @param jsonTxt
* @throws IOException void
*/
public static void responseJson(HttpServletResponse response, String jsonTxt)
throws IOException {
if (jsonTxt == null || response == null)
return;
response.addHeader("Cache-Control", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setContentType("text/json;charset=UTF-8");
response.getWriter().print(jsonTxt);
}
}
如下:
function check(){
var user = document.all.user.value;
var pass = document.all.pass.value;
var cnum=Math.ceil(Math.random()*100);
var valid = document.getElementById("check_code").value;
//$.ajax({url:"sysUser.do?method=login",cache:false,data:"user="+user+"&pass="+pass+"&date="+new Date(),success:function(state){
var param = {
user : user,
pass : pass,
valid : valid,
cnum : cnum
}
// 指定处理的路径
var http_value = jQuery.param(param);
var url = "<%=path%>/broadAction/login.do?radom="+cnum;
// 获得返回值
$.getJSON(url, http_value, function(josn){
if(josn.status=="2"){
alert("验证码错误,请重新输入");
return false;
}
else if(josn.status=="1"){
window.location="<%=path%>/securityAction/showNavigation.do?radom="+cnum+"";
}else{
alert("帐户信息不正确");
//prompt("帐户信息不正确");
//window.setTimeout("document.getElementById(\"prompt\").innerHTML=''",3000);
return false;
}
});
}
// 刷新验证码图片
function refresh(obj){
[color=red]var d = new Date(); //若是IE8有缓存,需要加上序列号避免缓存
obj.src="check?t="+d.toString(38);[/color]
}
验证码TR : <tr>
<td height="30" align="right" nowrap="nowrap" class="copy1">验证码: </td>
<td><input id="check_code" name="valid" type="text" size="6">
<a οnclick="refresh(imcheck);"><img id="imcheck" src="check"></a></td>
</tr>
后台Action
private JSONObject json;//JSON
json = new JSONObject();
if(check_code.equals(valid)){ //判断验证码是否正确
}else{
json.put("status", "2"); // 验证码错误,
JSonEx.responseJson(response, json);
}
JsonEx封装类:
public class JSonEx {
private static final Log logger = LogFactory.getLog(JSonEx.class);
/**
* 方法名称:responseJson
* 创建日期:Jul 4, 2009
* 方法描述:
* @param response
* @param jsonObject
* @throws IOException void
*/
public static void responseJson(HttpServletResponse response,
JSONObject jsonObject) throws IOException {
if (jsonObject == null || response == null)
return;
String jsonTxt = jsonObject.toString();
responseJson(response, jsonTxt);
}
/**
*
* 方法名称:responseJson
* 创建日期:Dec 11, 2008
* 方法描述:
* @param response
* @param jsonTxt
* @throws IOException void
*/
public static void responseJson(HttpServletResponse response, String jsonTxt)
throws IOException {
if (jsonTxt == null || response == null)
return;
response.addHeader("Cache-Control", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setContentType("text/json;charset=UTF-8");
response.getWriter().print(jsonTxt);
}
}