復制代碼 代碼如下:
function getHTTPRequest()
{
var xhr = false;
if (window.XMLHttpRequest)
xhr = new XMLHttpRequest(); //IE除外的浏覽器
else if (window.ActiveXObject)
{
try
{
xhr = new ActiveXObject("Msxm12.XMLHTTP");//最新版的ActiveX對象
}
catch(e)
{
try
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
xhr = false;
}
}
}
}
接下來是一個應用的實例:
復制代碼 代碼如下:
//完整的GET請求
var request = getHTTPRequest();
if(request)
{
request.onreadystatechange = dosomething;
request.open("GET","file.doc",true);
request.send(null);
}