日期:2014-05-16 浏览次数:20718 次
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type='text/javascript'>
var xmlHttp;
function createXMLHttpRequest() {
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function validate(field) {
/*var username=document.getElementById('username').value;
alert(username);
alert(username);*/
if(field.value.length!=0){
alert(field.value.length);}
if(field.value.length!=0){
//创建Ajax核心对象XMLHttpRequest;
alert(field.value);
createXMLHttpRequest();
var url="validate?userId="+field.value+"&time= "+new Date().getTime();
//设置请求方式为get, 设置请求的url,设置为异步提交
xmlHttp.open("GET",url,true);
//讲方法地址复制给onreadystatechange属性
xmlHttp.onreadystatechange=callback;
//讲设置信息发送到Ajax引擎
xmlHttp.send(null);
}
}
function callback(){
alert(xmlHttp.readyState);
if(xmlHttp.readyState ==4){
if(xmlHttp.status==200){
alert("请求成功!");
alert(xmlHttp.responseText);
}else{
alert("请求失败,错误码= "+xmlHttp.status);}
}
}
</script>
</head>
<body>
<form>
<label>用户名</label><input type='text' name='username' id='username'
onblur='validate(this)'>
</form>
</body>
</html>