创建xmlHTTPRequest对象
2010-03-31 14:16 爱研究源码的javaer 阅读(287) 评论(0) 编辑 收藏 举报function getRequest() {
http_request = false;
if (window.XMLHttpRequest) {
//对于Mozilla﹑Netscape﹑Safari等浏览器,创建XMLHttpRequest
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
//如果服务器响应的header不是text/xml,可以调用其它方法修改该header
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) {
// 对于Internet Explorer浏览器,创建XMLHttpRequest
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
return http_request;
}
2、第二种:
function getHttpRequest()
{
var xmlreq = false;
if (window.XMLHttpRequest) {
// Create XMLHttpRequest object in non-Microsoft browsers
xmlreq = new XMLHttpRequest();
} else if (window.ActiveXObject) {
// Create XMLHttpRequest via MS ActiveX
try {
// Try to create XMLHttpRequest in later versions
// of Internet Explorer
xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e1) {
// Failed to create required ActiveXObject
try {
// Try version supported by older versions
// of Internet Explorer
xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
// Unable to create an XMLHttpRequest with ActiveX
}
}
}
return xmlreq;
}
3.第三种:
function CreateXMLHttp()
{
if(typeof(XMLHttpRequest) != "undefined")
{
return new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
var
aVersions=["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];
for(var i=0;i<aVersions.length;i )
{
try
{
var oXmlHttp = new ActiveXObject(aVersions);
return oXmlHttp;
}
catch(oError)
{
//alert('Create XMLHttp failed!');
}
}
}
throw new Error('Create XMLHttp failed!');
}
转自:http://hi.baidu.com/qianjian21/blog/item/b09d58ece9f294d72f2e21cb.html