获取XMLHttpRequest对象

 1 //创建一个XMLHttpRequest对象 ,利用此对象与服务器进行通信 是AJAX技术的核心
 2 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
 3 function ajaxFunction(){
 4    var xmlHttp;
 5    try{ // Firefox, Opera 8.0+, Safari
 6         xmlHttp=new XMLHttpRequest();
 7     }
 8     catch (e){
 9        try{// Internet Explorer
10              xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
11           }
12         catch (e){
13           try{
14              xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
15           }
16           catch (e){}
17           }
18     }
19 
20     return xmlHttp;
21  }
22 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
23 function getXMLHttpRequest(){
24      var xmlHttpReq=null; 
25      if (window.ActiveXObject) {//IE浏览器创建XMLHttpRequest对象
26          xmlHttpReq = new ActiveXObject("MSXML2.XMLHTTP.3.0");
27      }else if(window.XMLHttpRequest){
28           xmlHttpReq = new XMLHttpRequest();
29      }
30      return xmlHttpReq;
31 }
32 
33 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
34 
35 /**
36  * 获取XmlHttpRequest对象
37  */
38 function getXMLHttpRequest() {
39     var xmlHttpReq=null;
40     if (window.XMLHttpRequest) {//Mozilla 浏览器
41         xmlHttpReq = new XMLHttpRequest();
42     }else {
43         if (window.ActiveXObject) {//IE 浏览器
44             try {
45                 xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
46             }
47             catch (e) {
48                 try {//IE 浏览器
49                     xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
50                 }
51                 catch (e) {
52                 }
53             }
54         }
55     }
56     return xmlHttpReq;
57 }
58 
59  
原文地址:https://www.cnblogs.com/0519xf/p/4782815.html