提供使用xmlhttp访问其他站点的范例。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>LEM User Build</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script>
var xmlhttp=false;
function GetXMLHTTP()
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
try
{
xmlhttp=new ActiveXObject("MSxml2.XMLHTTP.3.0");
}
catch(e)
{
xmlhttp=false;
}
}
return xmlhttp;
}
function validate(username)
{
GetXMLHTTP();
var url="http://reg.163.com/reg/reg0.jsp?username="+username;
xmlhttp.open("post",url,true);
xmlhttp.setRequestHeader("Content-Type","utf-8");
xmlhttp.onreadystatechange=callbackvalidate;
xmlhttp.send();
}
function getrandom(len)
{
var seed = new Array(
'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'0123456789'
);
var idx,i;
var result = '';
for (i=0; i<len; i++)
{
idx = Math.floor(Math.random()*3);
result += seed[idx].substr(Math.floor(Math.random()*(seed[idx].length)), 1);
}
return result;
}
function callbackvalidate()
{
if(xmlhttp.readyState==4)
{
if(xmlhttp.responseText.indexOf("用户名已经存在")>0)
{
document.getElementById("divinfo").innerHTML="用户名已经存在";
}
else
{
document.getElementById("divinfo").innerHTML="用户名不存在";
}
}
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<span id="divinfo" style="COLOR: blue; BACKGROUND-COLOR: #99ff33">dd</span>
<FONT face="宋体" id="FONT1" runat="server">
<br>
长度:
<SELECT id="sel" NAME="sel">
<option value="1">1</option>
<option value="3">3</option>
<option value="5" selected>5</option>
<option value="7">7</option>
<option value="9">9</option>
</SELECT>
<BR>
生成: <INPUT TYPE="text" id="txt" NAME="txt"> <INPUT TYPE="button" VALUE=" GO " ONCLICK="txt.value=getrandom(sel.value);validate(txt.value)"
ID="Button1" NAME="Button1"> </FONT>
</form>
</body>
</HTML>