ajax 实例
jsp页面代码:
<script type="text/javascript">
var xmlHttp;
function createXMLHttp(){
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}else{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
function checkUsername(){
var username=document.getElementById("user_name").value;
var url="UserAction.action?method=checkUsername&user_name="+username;
createXMLHttp();
xmlHttp.onreadystatechange=checkUsernameCallback;
xmlHttp.open("post",url,true);
xmlHttp.send();
document.getElementById("info").innerHTML="正在验证······";
}
function checkUsernameCallback(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var flag=xmlHttp.responseText;
if(flag==1){
document.getElementById("info").innerHTML="输入正确!";
document.getElementById("info").style.color="green";
}else{
document.getElementById("info").innerHTML="输入有误!";
document.getElementById("info").style.color="red";
document.getElementById("user_name").focus();
}
}else{
document.getElementById("info").innerHTML="输入有误!";
document.getElementById("info").style.color="red";
document.getElementById("user_name").focus();
}
}
}
</script>
后台action代码:
try {
/** 获取response对象 */
HttpServletResponse response = ServletActionContext.getResponse();
/** 获取输出out对象 */
PrintWriter out = response.getWriter();
if(!user.equals(null)){
out.print(1);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。