首先是jsp页面和脚本,为了方便写在一个里面
这是一个很常见的检测用户名是否存在的功能
这里用的是struts
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
ajax
</title>
</head>
<body bgcolor="#ffffff">
<h1>
<input name="username" type="text" maxlength="20" />
<input id="chk-name-btn" type="button" value="检测帐号" onclick="testName('<%=request.getContextPath()%>')" />
<div id="view_name"></div>
</h1>
</body>
</html>
<script language="javascript">
if (window.ActiveXObject && !window.XMLHttpRequest) {
window.XMLHttpRequest=function() {
return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') != -1) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP');
};
}//取得XMLHttpRequest对象