How to Create ajax objects and create multiple ajax objects compatible with multiple browsers
This article describes how to Create ajax objects and make them compatible with multiple browsers. For more information, see
<Script> function createAjax () {var request = false; // XMLHttpRequest in the window object is non-IE, including (IE7 or later) if (window. XMLHttpRequest) {request = new XMLHttpRequest (); if (request. overrideMimeType) {request. overrideMimeType ("text/xml");} // The ActiveXObject attribute in the window object is IE} else if (window. activeXObject) {var versions = ['Microsoft. XMLHTTP ', 'msxml. XMLHTTP ', 'msxml2. XMLHTTP.7.0 ', 'msxml2. XMLHTTP.6.0 ', 'msxml2. XMLHTTP.5.0 ', 'msxml2. XMLHTTP.4.0 ', 'msxml2. XMLHTTP.3.0 ', 'msxml2. XMLHTTP ']; for (var I = 0; I <versions. length; I ++) {try {request = new ActiveXObject (versions [I]); if (request) {return request ;}} catch (e) {request = false ;}}return request;} var ajax = createAjax (); alert (ajax); </script>
How to create a common AJAX object
Determine whether the server supports ajax
Function tel (){
Var tel = false;
Try {
Tel = new XMLHttpRequest ();
} Catch (e ){
// For IE browsers
Try {
Tel = new ActiveXObject ('msxml2. xmlhttp'); // ie8
} Catch (e ){
Try {
Tel = new ActiveXObject ('Microsoft. xmlhttp ');
} Catch (e ){
Alert ('your browser does not support ajax ');
}
}
}
Return tel;
}
Method 2:
Function tel (){
Var tel = false;
If (window. XMLHttpRequest ){
Tel = new XMLHttpRequest ();
} Else {
Tel = new window. ActiveXObject ('Microsoft. xmlhttp ');
}
Return tel;
}
Browser compatibility issues of the js ajax setrequestheader method
How is xmlhttp created?
The xmlhttp object you created in Firefox is unsuccessful. Only create in IE
In IE and Firefox, the method for creating the xmlhttp object is different. Now you can use this method to create this object. You can try it:
Function createxmlhttp () {// create an xmlhttp object
Var xmlhttp = false;
Try {
Xmlhttp = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (e ){
Try {
Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (e ){
Xmlhttp = false;
}
}
If (! Xmlhttp & typeof XMLHttpRequest! = 'Undefined '){
Xmlhttp = new XMLHttpRequest ();
If (xmlhttp. overrideMimeType ){
Xmlhttp. overrideMimeType ('text/html '); // you can specify the MiME type.
}
}
Return xmlhttp;
}
Then, call the following in your AJAX method:
Var xmlhttp = createxmlhttp ();
If (! Xmlhttp ){
Alert ("your browser does not support XMLHTTP !! ");
Return;
}
Xmlhttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
The above is my AJAX implementation method. After testing, Firefox/IE and other browsers are easy to use.
Question asked