´´½¨Ò»¸öXMLHTTP¶ÔÏó
Microsoft Internet Explorerä¯ÀÀÆ÷ʹÓÃMSXML½âÎöÆ÷´¦ÀíXML£¬MSXMLʵ¼ÊÉÏÓÐÁ½ÖÖ²»Í¬µÄ°æ±¾xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
¶ÔÓÚ·Ç Microsoft ä¯ÀÀÆ÷(Mozilla¡¢Firefox¡¢Safari¡¢Opera):
xmlHttp = new XMLHttpRequest();
function getXmlHttp()
{
var xmlHttp = null;
if ( window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
if (xmlHttp.overrideMimeType)
xmlHttp.overrideMimeType("text/xml");
}
else if ( window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
if ( !xmlHttp)
{
window.alert("XmlHttp not be Supported!");
}
return xmlHttp;
}
ÏÂÃæµÄ´úÂë˵Ã÷ÁËÈçºÎ´Ó´´½¨µ½³õʼ»¯XMLHTTPµ½·¢ËÍÊý¾ÝµÄ¹ý³Ì£¬²¢Ö¸¶¨´¦Àí·µ»ØÊý¾ÝµÄʼþ¾ä±ú
var xmlHttp = null;
function callServer(url)
{
xmlHttp = getXmlHttp();
xmlHttp.onReadyStateChange = setPage;
xmlHttp.open("GET",url,true);
xmlHttp.send();
document.getElementById("loading").innerHTML = "ÕýÔÚ¼ÓÔØ...";
}
µÃµ½·µ»ØÊý¾Ý²¢¸üÐÂÒ³Ãæ
function setPage()
{
switch(xmlHttp.readyState)
{
case 0:
document.getElementById("loading").innerHTML = "ÕýÔÚ³õʼ»¯...";
break;
case 1:
document.getElementById("loading").innerHTML = "ÕýÔÚ¼ÓÔØ...";
break;
case 2:
document.getElementById("loading").innerHTML = "ÒѼÓÔØÍê³É...";
break;
case 3:
document.getElementById("loading").innerHTML = "ÕýÔÚ½ÓÊÕÊý¾Ý...";
break;
case 4:
document.getElementById("loading").innerHTML = "ÒÑÍê³É!";
break;
}
if ( xmlHttp.readyState == 4)
{
if ( xmlHttp.status == 200)
{
document.getElementById("content").innerText = xmlHttp.responseText;
}
xmlHttp.abort();
}
}
HTML £º
<input name="url" id="url" type="text" value="http://www.sohu.com"><input type="button" onclick="callServer(document.getElementById('url').value)" value="get data"> <BR>
<div id="loading" style="color:red; font-size:12px;"></div>
<span id="content"></span>