苏朋老师的例子。该例子实现到www.163.com上验证一个用户是否注册。
首先注册并得到xmlhttp对象。然后提交并得到返回值。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/jscript">
//检测浏览器,创建xmlhttp-request对象
function getXMLRequester()
{
var xmlhttp_request = false;
try
{
if(window.ActiveXObject)
{
for(var i = 5;i;i--)
{
try
{
if(i==2)
{
xmlhttp_request = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
xmlhttp_request = new ActiveXObject("Msxml2.XMLHTTP" +i+"0");
xmlhttp_request.setRequestHeader("Content-Type","text-xml");
xmlhttp_request.setRequestHeader("Content_Type","gb2312");
}
break;
}
catch(e)
{
xmlhttp_request=false;
}
}
}
else if(window.XMLHttpRequest)
{
xmlhttp_request = new XMLHttpRequest();
if(xmlhttp_request.overrideMineType)
{
xmlhttp_request.overrideMineType("text/xml");
}
}
}
catch(e)
{
xmlhttp_request = false;
}
return xmlhttp_request;
}
function IDRequest(n)
{
url = n + document.getElementById("txtUserID").value;//定义网址参数
xmlhttp_request = getXMLRequester();
xmlhttp_request.onreadystatechange = doContents;//调用doContent函数;
xmlhttp_request.open("Get",url,true);
xmlhttp_request.send(null);
}
function doContents()
{
if(xmlhttp_request.readyState == 4)//收到完整的服务器响应
{
if(xmlhttp_request.status == 200)//Http服务器响应ok
document.getElementById("message").innerHTML = xmlhttp_request.responseText;//服务器响应的值
else
alert(http_request.status);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="txtUserID" style="font-size:12px;" />
<input type="button" id="btnOk" value="校验" style="font-size:12px" onclick ="IDRequest('http://reg.163.com/register/checkssn.jsp?username=')" />
<div id="message"></div>
</div>
</form>
</body>
</html>