Recently I was developing a server program using ajax and found that the IE browser did not support the xmlhttprequest object and the Microsoft.XMLHTTP control could not be found.
A problem arises that we need to solve. The solution is as follows:
1. Run regsvr32 msxml3.dll;
2. Use ready-made frameworks to do ajax;
3. Code optimization:
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);
or determine the browser
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;
}
or
<script language="javascript">
var http_request = false;
function send_request(url) {//Initialization, designated processing function, function to send request
http_request = false;
//Start initializing the XMLHttpRequest object
if(window.XMLHttpRequest) { //Mozilla browser
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//Set MiME category
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // IE browser
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) { //Exception, failed to create object instance
window.alert("Cannot create XMLHttpRequest object instance.");
return false;
}
http_request.onreadystatechange = processRequest;
// Determine the method and URL of sending the request and whether to execute the next code synchronously
http_request.open("GET", url, true);
http_request.send(null);
}
// Function that handles returned information
function processRequest() {
if (http_request.readyState == 4) { // Determine object status
if (http_request.status == 200) { //The information has been returned successfully, start processing the information
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++) { // Loop to read the contents of employees.xml
var employee = employees[i];
feedbackStr += "Employee:" + employee.getAttribute("name");//Get the specified attribute of the label
feedbackStr += "Position:" + employee.getElementsByTagName("job")[0].firstChild.data;//Get the first data of the specified tag
feedbackStr += "Salary:" + employee.getElementsByTagName("salary")[0].firstChild.data;
feedbackStr += "rn";
}
alert(feedbackStr);
} else { //The page is abnormal
alert("There is an exception in the page you requested.");
}
}
}
</script>
http://www.cnblogs.com/skylaugh/archive/2006/11/20/566164.html