最近在用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;
}
或
<script language="javascript">
var http_request = false;
function send_request(url) {//初始化、指定處理函數、發送請求的函數
http_request = false;
//開始初始化XMLHttpRequest對象
if(window.XMLHttpRequest) { //Mozilla 瀏覽器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//設定MiME類別
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // IE瀏覽器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) { // 異常,建立物件實例失敗
window.alert("不能建立XMLHttpRequest物件實例.");
return false;
}
http_request.onreadystatechange = processRequest;
// 決定發送請求的方式和URL以及是否同步執行下段程式碼
http_request.open("GET", url, true);
http_request.send(null);
}
// 處理傳回訊息的函數
function processRequest() {
if (http_request.readyState == 4) { // 判斷物件狀態
if (http_request.status == 200) { // 訊息已成功返回,開始處理訊息
var returnObj = http_request.responseXML;
var xmlobj = http_request.responseXML;
var employees = xmlobj.getElementsByTagName("employee");
var feedbackStr = "";
for(var i=0;i<employees.length;i++) { // 迴圈讀取employees.xml的內容
var employee = employees[i];
feedbackStr += "員工:" + employee.getAttribute("name");//取得標籤指定屬性
feedbackStr += " 職位:" + employee.getElementsByTagName("job")[0].firstChild.data;//取得指定標籤的最初數據
feedbackStr += " 薪水:" + employee.getElementsByTagName("salary")[0].firstChild.data;
feedbackStr += "rn";
}
alert(feedbackStr);
} else { //頁面不正常
alert("您所要求的頁面有異常。");
}
}
}
</script>
http://www.cnblogs.com/skylaugh/archive/2006/11/20/566164.html