ajax

<%@ page language="java" contentType="text/html;charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ include file="baseReference.jsp"%>
<%@ taglib prefix="spring" uri="../tld/spring.tld"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<form name="form" action="${basePath}/user/userInformation.action" method="post">

用户名:<input type="text" id="username" name="username"/>
密&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" id="password" name="password"/>
验证码:<input type="text" id="checkcode" name="checkcode"/><img id="code" src="${basePath}/user/createCode.action"><a href="#" onclick="changeCode()">看不清,换一张</a>
<input type="button" name="sm" value="登录" onclick="checkRegisted()">
<a href="${basePath}/user/register.action">注&nbsp;&nbsp;册</a>



</form>

</body>
<script type="text/javascript">
function checkRegisted() {
var username = $("#username").val();
var password = $("#password").val();
if(null == username || null == password) {
alert("用户名或密码不能为空!");
return;
}
var url = "${basePath}/user/checkRegisted.action";
$.post(url,{username : username,password : password}, function(result) {
if("false" == result){
alert("用户名或密码错误!或者权限异常!");
} else {
alert("欢迎登陆!");
document.form.submit();
}
});

    var url = "${basePath}/user/checkCode.action";
$.post(url,{checkcode : checkcode}, function(result) {
if("true" == result) {
alert("ok!");
return;
} else {
alert("RE");
}
});
}

function getXMLRequest(){ 
    var request; 
    try{ 
        request = new XMLHttpRequest(); 
    }catch(e){ 
        try{ 
request = new ActiveXObject("Microsoft.XMLHttp");
        }catch(e){ 
alert("ŒAJAX!!!");
            return null; 
        } 
    } 
    return request; 


//改变验证码
function changeCode(){
    var request = getXMLRequest();//XMLHttpRequest
    request.onreadystatechange = function(){ 
        if(request.readyState == 4){ 
document.getElementById("code").src = "${basePath}/user/createCode.action?" + new Date();
        } 
    };          
request.open("GET","${basePath}/user/createCode.action",true);
    request.send(null);


</script>
</html>

你可能感兴趣的:(Ajax)