最近在用AJax開發服務器程序,發現IE浏覽器不支持xmlhttprequest對象,而且找不到Microsoft.XMLHTTP控件。
問題出現了我們需要解決,解決方案如下:
1、運行下regsvr32 msXML3.dll;
2、用現成的框架來做AJax;
3、代碼優化:
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
if(handle_s == null)
handle_s = "bin/normal.py/db";
this.XMLHttp.onreadystatechange = handle_l;
this.XMLHttp.open("GET",handle_s,true);
this.XMLHttp.send(null);
或判斷浏覽器
var agt = navigator.userAgent.toLowerCase();
var is_ie = (agt.indexOf("msIE") != -1);
var is_ie5 = (agt.indexOf("msIE 5") != -1);
var is_opera = (agt.indexOf("Opera") != -1);
var is_mac = (agt.indexOf("Mac") != -1);
var is_gecko = (agt.indexOf("gecko") != -1);
var is_safari = (agt.indexOf("safari") != -1);
function CreateXMLHttpReq(handler) {
var XMLhttp = null;
if (is_IE) {
// Guaranteed to be ie5 or IE6
var control = (is_IE5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
try {
XMLhttp = new ActiveXObject(control);
XMLhttp.onreadystatechange = handler;
} catch (ex) {
// TODO: better help message
alert("You need to enable active scripting and activeX controls");
}
} else {
// Mozilla
xmlhttp = new XMLHttpRequest();
XMLhttp.onload = handler;
XMLhttp.onerror = handler;
}
return XMLhttp;
}
或者