test Ajax (XMLHttpRequest)

    技术2022-05-12  0

    <!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>


    最新回复(0)