HttpRequest.prototype.createXmlHttpRequest = function ()
{
if (window.XMLHttpRequest)
{
var oHttp = new XMLHttpRequest();
return oHttp;
}
else if (window.ActiveXObject)
{
var versions =
[
"MSXML2.XmlHttp.6.0",
"MSXML2.XmlHttp.3.0"
];
for (var i = 0; i < versions.length; i++)
{
try
{
var oHttp = new ActiveXObject(versions[i]);
return oHttp;
}
catch (error)
{
//do nothing here
}
}
}
alert("Your browser doesn’t support XMLHttp");
}
二、发送请求
// 请求服务器
function HttpRequest(sUrl, fpCallback)
{
this.request = this.createXmlHttpRequest();//创建请求对象
this.request.open("GET", sUrl, true);//请求方式,地址,是否异步请求:
var tempRequest = this.request;
function request_readystatechange()
{
if (tempRequest.readyState == 4)
{
if (tempRequest.status == 200)
{
fpCallback(tempRequest.responseText);
}
else
{
alert("An error occurred trying to contact the server.");
}
}
}
this.request.onreadystatechange = request_readystatechange;
}
HttpRequest.prototype.send = function ()
{
this.request.send(null);
}