function creatXmlHttpRequest()
{
if (typeof XMLHttpRequest != 'undefined')
{
return new XMLHttpRequest();
}
else if (typeof ActiveXObject != 'undefined')
{
var MSXML = ['MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP',
'Microsoft.XMLHTTP'];
for (var i = 0; MSXML.length; i ++)
{
try{
return new ActiveXObject(version[i]);
}catch (e)
{
//.......
}
}
}
else
{
throw new Error('ÄúµÄϵͳ»òä¯ÀÀÆ÷²»Ö§³ÖXHR¶ÔÏó£¡');
}
}
GET·½Ê½»Ø´«json
function checkUserByAjaxGet()
{
// µÚÒ»²½£¬µÃµ½Ò»¸öXMLHttpRequest¶ÔÏó
// var xhr=new XMLHttpRequest();
// Èç¹û˵IE6£¬¾ÍÐèҪʹÓÃActive×齨
// xmlhttp=new ActiveXObject(MSXML[n]);
var xhr = creatXmlHttpRequest();
var name = document.rgform.username.value;
// ÉèÖÃÒ»¸öʼþµÄ¼àÌýº¯Êý
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4)
{
if (xhr.status == 200 || xhr.status == 304)
{
var ret = xhr.responseText;
var _ret = eval('(' + ret + ')');
// var _ret = JSON.parse(ret);
document.getElementById("msg").innerHTML = _ret.tip;
if (_ret.success = true)
{
document.rgform.username.focus();
}
}
}
}
// µÚ¶þ²½£¬×¼±¸Ò»¸öÁ¬½ÓÇëÇó
xhr.open("get", "checkuserByJSON.jsp?name=" + name, true);
// µÚÈý²½£¬·¢ÆðÇëÇó
xhr.send(null);
}
POST·½Ê½»Ø´«json
function checkUserByAjaxPost()
{
// µÚÒ»²½,´´½¨xhr¶ÔÏó
var xhr = creatXmlHttpRequest();
var name = document.rgform.username.value;
// µÚ¶þ²½,ÉèÖÃÒ»¸öʼþµÄ¼àÌýº¯Êý
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4)
{
if (xhr.status == 200 || xhr.status == 304)
{
var ret = xhr.responseText;
var _ret = eval('(' + ret + ')');
// var _ret = JSON.parse(ret);
document.getElementById("msg").innerHTML = _ret.tip;
if (_ret.success = true)
{
document.rgform.username.focus();
alert(xhr.getResponseHeader("Content-Length"));
alert(xhr.getResponseHeader("Content-Type"));
alert(xhr.getResponseHeader("Date"));
alert(xhr.getResponseHeader("Server"));
}
}
}
}
// µÚÈý²½,×¼±¸Ò»¸öPOSTÁ¬½ÓÇëÇó
xhr.open("post", "checkuserByJSON.jsp", true);
// ʹÓÃpost·½Ê½Ìá½»£¬±ØÐëÒª¼ÓÉÏÈçÏÂÒ»ÐÐ
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// µÚËIJ½,·¢ÆðÇëÇó
xhr.send("name=" + name + "&password=ppppp");
}
POST·½Ê½»Ø´«xml
function checkUserByAjaxPostXml() {
// µÚÒ»²½,´´½¨xhr¶ÔÏó
var xhr = creatXmlHttpRequest();
var name = document.rgform.username.value;
// µÚ¶þ²½,ÉèÖÃÒ»¸öʼþµÄ¼àÌýº¯Êý
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4)
{
if (xhr.status == 200 || xhr.status == 304)
{
var ret = xhr.responseXML;
var successNode = ret.getElementsByTagName("success")[0];
var tipNode = ret.getElementsByTagName("tip")[0];
document.getElementById("msg").innerHTML = tipNode.firstChild.nodeValue;
if (successNode.firstChild.nodeValue == true)
{
document.rgform.username.focus();
}
}
}
}
// µÚÈý²½,×¼±¸Ò»¸öPOSTÁ¬½ÓÇëÇó
xhr.open("post", "checkuserByXML.jsp", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// µÚËIJ½,·¢ÆðÇëÇó
xhr.send("name=" + name + "&password=ppppp");
}
·µ»ØµÄµÄjson
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<%
String username = request.getParameter("username");
response.setContentType("application/json;charset=UTF-8");
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-cache");
if("admin".equals(username))
{
response.getWriter().write("{\"success\":"+true+",\"tip\":\"Óû§ÃûÒÑ´æÔÚ\"}");
}
else
{
response.getWriter().write("{\"success\":"+false+",\"tip\":\"Óû§Ãû¿ÉÒÔʹÓÃ\"}");
}
%>
·µ»ØµÄxml
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<%
String userId = request.getParameter("name");
response.setContentType("application/xml;charset=UTF-8");
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-cache");
if("admin".equals(userId))
{
response.getWriter().write("<root><success>true</success><tip>Óû§ÃûÒÑ´æÔÚ</tip></root>");
}
else
{
response.getWriter().write("<root><success>false</success><tip>Óû§Ãû¿ÉÒÔʹÓÃ</tip></root>");
}
%>
½çÃæ
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>register.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="js/register.js"></script>
<script type="text/javascript" src="js/jquery-1.5.2.js"></script>
<style type="text/css">
#round {
padding:10px; width:300px; height:150px;
border:1px solid #000;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius:10px;
margin:10px auto;
}
#round div.user, #round div.pass {
font-size:14px;
color:#666;
padding:15px 0;
text-align:center;
position: relative;
}
#round span{
position: absolute;
color:red;
top:35px;
left:100px;
}
#round input.text {
width:200px;
height:25px;
border:1px solid #ccc;
background:#fff;
font-size:14px;
}
#round .button {
padding:5px 0;
text-align:center;
}
</style>
</head>
<body>
<div>
<form method="post" action="" >
<div>
Óû§Ãû:<input type="text" onchange="ʼþµ÷Óõķ½·¨">
<span></span>
</div>
<div>
ÃÜ Âë:<input type="password">
</div>
<div><input type="button" value="Ìá½»"/></div>
</form>
</div>
</body>
</html>
Ч¹ûͼ
¡¡