test Ajax (XMLHttpRequest)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Ajax 状态测试</title>

    <script type="text/javascript">
 


      var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
{
var xmlHttp;
try
{
   xmlHttp = new XMLHttpRequest();
}
catch(e)
{
   var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
           "MSXML2.XMLHTTP.5.0",
           "MSXML2.XMLHTTP.4.0",
           "MSXML2.XMLHTTP.3.0",
           "MSXML2.XMLHTTP",
           "Microsoft.XMLHTTP");
   for (var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++){
    try
    {
     xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
    }
    catch (e) {}
   }
}
if (!xmlHttp)
   alert("建立XMLHttpRequest object错误。");
else
   return xmlHttp;
}

function process()
{
if (xmlHttp)
{
   try
   {
    xmlHttp.open("GET","http://www.baidu.com",false);
    xmlHttp.onreadystatechange = handleRequestStateChange;
    xmlHttp.send(null);
   }
   catch (e)
   {
    alert("不能连接服务器:/n" + e.toString());
   }
}
}

function handleRequestStateChange()
{
myDiv = document.getElementById("myDivElement");
if (xmlHttp.readyState == 1)
{
   myDiv.innerHTML += "Requset status:1 (loading) <br />"
}
else if(xmlHttp.readyState == 2)
{
   myDiv.innerHTML += "Requset status:2 (loading) <br />"
}
else if(xmlHttp.readyState == 3)
{
   myDiv.innerHTML += "Requset status:3 (loading) <br />"
}
else if(xmlHttp.readyState == 4)
{
   if (xmlHttp.status == 200)
   {
    try
    {
     response = xmlHttp.responseText;
     myDiv.innerHTML += "Requset status:4 (complete).服务器返回:<br />"
     myDiv.innerHTML = response;
    }
    catch (e)
    {
     alert("读取response错误:" + e.toString());
    }
   }
   else
   {
    alert("接收数据时出现问题/n" + xmlHttp.statusText);
   }
}
}

 

function Button1_onclick() {
process();

 

}

    </script>

</head>
<body>
    你好!服务器!
    <br />
    <input id="Button1" type="button" value="button" οnclick="return Button1_onclick()" />
    <div id="myDivElement">
        yy
    </div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值