AJAX

 

 1  <script type="text/javascript">
 2             function  createXmlHttpRequest() {
 3                 if (window.ActiveXObject) {
 4                     return new ActiveXObject("Microsoft.XMLHTTP");
 5                 } else if (window.XMLHttpRequest) {
 6                     return new XMLHttpRequest();
 7   
 8                 }
 9 
10             }
11 
12             var xhr;//声明一个全局变量
13             function create() {
14                 //创建XMLHttpRequest对象
15                 xhr = createXmlHttpRequest();
16                 //初始化XMLHttpRequest
17                 xhr.open("POST", "AJAXHandler.ashx", true);
18                 //设置浏览器不适用缓存
19                 xhr.sentRequestHeard("If-Modified-Since", "0");
20                 //post提交
21                 xhr.sentRequestHeard("Content-Type","application/x-form-urlencoded");
22                 //设置回调函数
23                 xhr.onreadystatechange = function () {
24                     //XMLHttpRequest状态属性
25                     if (xhr.readyState == 4) {
26                         if (status == 200) {
27                             alert(xhr.responseText); 
28                         } else {
29                             alert("系统繁忙,请联系管理员!");
30                         }
31                     }
32                 };
33                 xhr.send(null);
34 
35             }

 

 1  <script type="text/javascript">
 2             function  createXmlHttpRequest() {
 3                 if (window.ActiveXObject) {
 4                     return new ActiveXObject("Microsoft.XMLHTTP");
 5                 } else if (window.XMLHttpRequest) {
 6                     return new XMLHttpRequest();
 7   
 8                 }
 9 
10             }
11 
12             var xhr;//声明一个全局变量
13             function create() {
14                 //创建XMLHttpRequest对象
15                 xhr = createXmlHttpRequest();
16                 //初始化XMLHttpRequest
17                 xhr.open("POST", "AJAXHandler.ashx", true);
18                 //设置浏览器不适用缓存
19                 xhr.sentRequestHeard("If-Modified-Since", "0");
20                 //post提交
21                 xhr.sentRequestHeard("Content-Type","application/x-form-urlencoded");
22                 //设置回调函数
23                 xhr.onreadystatechange = function () {
24                     //XMLHttpRequest状态属性
25                     if (xhr.readyState == 4) {
26                         if (status == 200) {
27                             alert(xhr.responseText); 
28                         } else {
29                             alert("系统繁忙,请联系管理员!");
30                         }
31                     }
32                 };
33                 xhr.send(null);
34 
35             }

 

在Ajax应用中创建万能的XmlHttpRequest对象

目前主要针对浏览器内核的不同,采取不同的方式创建XHR对象。

      对于IE浏览器,Microsoft 浏览器 Internet Explorer 使用 MSXML 解析器处理 XML。因此如果编写的 Ajax 应用程序要和 Internet Explorer 打交道,那么必须用一种特殊的方式创建对象。方法如下:

 1 var xmlHttp = false;
 2 try {
 3 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
 4 } catch (e) {
 5 try {
 6 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
 7 } catch (e2) {
 8 xmlHttp = false;
 9 }
10 }

 而对于非IE内核浏览器,需要使用不同的代码。事实上就是如下所示的简单代码:
      var xmlHttp = new XMLHttpRequest();
      这行简单得多的代码在 Mozilla、Firefox、Safari、Opera 以及基本上所有以任何形式或方式支持 Ajax 的非 Microsoft 浏览器中,创建了 XMLHttpRequest 对象。
      后来发现,我这个写法是最基础的,而XMLHTTP组件这几年又随着OS的更新而不断升级,推出了很多新的版本。在我的TopMapTest项目中,我找到我原来用过的一个xmlextra.js封装的脚本库发现,在这个功能类中,也提供了对不同浏览器创建XHR对象的扩展,其内容为:

  1 //<script>
  2 //////////////////
  3 // Helper Stuff //
  4 //////////////////
  5 
  6 // used to find the Automation server name
  7 function getDomDocumentPrefix() {
  8     if (getDomDocumentPrefix.prefix)
  9         return getDomDocumentPrefix.prefix;
 10     
 11     var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
 12     var o;
 13     for (var i = 0; i < prefixes.length; i++) {
 14         try{
 1 /*****************************************************************************************   
 2   Object   CreateHTTPPoster(void)   
 3   创建尽可能高版本的XMLHTTP对象   
 4   *****************************************************************************************/   
 5   function   CreateHTTPPoster(){   
 6   if(window.XMLHttpRequest)   return   new   XMLHttpRequest();   
 7   try{   
 8   return   new   ActiveXObject('MSXML2.XMLHTTP.4.0');   
 9   }catch(e){try{   
10   return   new   ActiveXObject('MSXML2.XMLHTTP.3.0');   
11   }catch(e){try{   
12   return   new   ActiveXObject('MSXML2.XMLHTTP.2.6');   
13   }catch(e){try{   
14   return   new   ActiveXObject('MSXML2.XMLHTTP');   
15   }catch(e){try{   
16   return   new   ActiveXObject('Microsoft.XMLHTTP');   
17   }catch(e){return   null;}}}}}   
18   }   

 

 15             // try to create the objects
 16             o = new ActiveXObject(prefixes[i] + ".DomDocument");
 17             return getDomDocumentPrefix.prefix = prefixes[i];
 18         }
 19         catch (ex) {};
 20     }
 21     
 22     throw new Error("Could not find an installed XML parser");
 23 }
 24 
 25 function getXmlHttpPrefix() {
 26     if (getXmlHttpPrefix.prefix)
 27         return getXmlHttpPrefix.prefix;
 28     
 29     var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
 30     var o;
 31     for (var i = 0; i < prefixes.length; i++) {
 32         try {
 33             // try to create the objects
 34             o = new ActiveXObject(prefixes[i] + ".XmlHttp");
 35             return getXmlHttpPrefix.prefix = prefixes[i];
 36         }
 37         catch (ex) {};
 38     }
 39     
 40     throw new Error("Could not find an installed XML parser");
 41 }
 42 
 43 //////////////////////////
 44 // Start the Real stuff //
 45 //////////////////////////
 46 
 47 
 48 // XmlHttp factory
 49 function XmlHttp() {}
 50 
 51 XmlHttp.create = function () {
 52     try {
 53         if (window.XMLHttpRequest) {
 54             var req = new XMLHttpRequest();
 55             
 56             // some versions of Moz do not support the readyState property
 57             // and the onreadystate event so we patch it!
 58             if (req.readyState == null) {
 59                 req.readyState = 1;
 60                 req.addEventListener("load", function () {
 61                     req.readyState = 4;
 62                     if (typeof req.onreadystatechange == "function")
 63                         req.onreadystatechange();
 64                 }, false);
 65             }
 66             
 67             return req;
 68         }
 69         if (window.ActiveXObject) {
 70             return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
 71         }
 72     }
 73     catch (ex) {}
 74     // fell through
 75     throw new Error("Your browser does not support XmlHttp objects");
 76 };
 77 
 78 // XmlDocument factory
 79 function XmlDocument() {}
 80 
 81 XmlDocument.create = function () {
 82     try {
 83         // DOM2
 84         if (document.implementation && document.implementation.createDocument) {
 85             var doc = document.implementation.createDocument("", "", null);
 86             
 87             // some versions of Moz do not support the readyState property
 88             // and the onreadystate event so we patch it!
 89             if (doc.readyState == null) {
 90                 doc.readyState = 1;
 91                 doc.addEventListener("load", function () {
 92                     doc.readyState = 4;
 93                     if (typeof doc.onreadystatechange == "function")
 94                         doc.onreadystatechange();
 95                 }, false);
 96             }
 97             
 98             return doc;
 99         }
100         if (window.ActiveXObject)
101             return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
102     }
103     catch (ex) {}
104     throw new Error("Your browser does not support XmlDocument objects");
105 };
106 
107 // Create the loadXML method and xml getter for Mozilla
108 if (window.DOMParser &&
109     window.XMLSerializer &&
110     window.Node && Node.prototype && Node.prototype.__defineGetter__) {
111 
112     // XMLDocument did not extend the Document interface in some versions
113     // of Mozilla. Extend both!
114     XMLDocument.prototype.loadXML = 
115     Document.prototype.loadXML = function (s) {
116         
117         // parse the string to a new doc    
118         var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
119         
120         // remove all initial children
121         while (this.hasChildNodes())
122             this.removeChild(this.lastChild);
123             
124         // insert and import nodes
125         for (var i = 0; i < doc2.childNodes.length; i++) {
126             this.appendChild(this.importNode(doc2.childNodes[i], true));
127         }
128     };
129     
130     
131     /*
132      * xml getter
133      *
134      * This serializes the DOM tree to an XML String
135      *
136      * Usage: var sXml = oNode.xml
137      *
138      */
139     // XMLDocument did not extend the Document interface in some versions
140     // of Mozilla. Extend both!
141     XMLDocument.prototype.__defineGetter__("xml", function () {
142         return (new XMLSerializer()).serializeToString(this);
143     });
144     Document.prototype.__defineGetter__("xml", function () {
145         return (new XMLSerializer()).serializeToString(this);
146     });
147 }

在上面的getXmlHttpPrefix()方法中,我们发现["MSXML2", "Microsoft", "MSXML", "MSXML3"];所指定的4个前缀,在创建IE浏览器支持的XHR对象时,提供了不同的创建方法。但如果要在代码中,换一种方式来写,应该如何写呢?这几种前缀+".XMLHttp"所创建的对象分别有什么区别呢?所以在网上查了一下,关于这方面的内容还蛮多,特将部分内容记录:

片断1:

 1 /*****************************************************************************************   
 2   Object   CreateHTTPPoster(void)   
 3   创建尽可能高版本的XMLHTTP对象   
 4   *****************************************************************************************/   
 5   function   CreateHTTPPoster(){   
 6   if(window.XMLHttpRequest)   return   new   XMLHttpRequest();   
 7   try{   
 8   return   new   ActiveXObject('MSXML2.XMLHTTP.4.0');   
 9   }catch(e){try{   
10   return   new   ActiveXObject('MSXML2.XMLHTTP.3.0');   
11   }catch(e){try{   
12   return   new   ActiveXObject('MSXML2.XMLHTTP.2.6');   
13   }catch(e){try{   
14   return   new   ActiveXObject('MSXML2.XMLHTTP');   
15   }catch(e){try{   
16   return   new   ActiveXObject('Microsoft.XMLHTTP');   
17   }catch(e){return   null;}}}}}   
18   }   

片断2:

 1 function   CreateHTTP()   
 2   {   
 3           if   (window.XMLHttpRequest)   return(new   XMLHttpRequest());   
 4           var   arr_t   =   new   Array   
 5           (   
 6                   "MSXML2.XMLHTTP.4.0",     
 7                   "MSXML2.XMLHTTP.3.0",     
 8                   "MSXML2.XMLHTTP.2.6",     
 9                   "Microsoft.XMLHTTP",     
10                   "MSXML.XMLHTTP"   
11           );   
12           for   (var   i=0;   i<arr_t.length;   i++)   
13           {   
14                   try   
15                   {   
16                           return(new   ActiveXObject(arr_t[i]));   
17                   }   
18                   catch(e)   
19                   {   
20                   }   
21           }   
22           return(null);   
23   }   

 

 1  <script type="text/javascript">
 2             function  createXmlHttpRequest() {
 3                 if (window.ActiveXObject) {
 4                     return new ActiveXObject("Microsoft.XMLHTTP");
 5                 } else if (window.XMLHttpRequest) {
 6                     return new XMLHttpRequest();
 7   
 8                 }
 9 
10             }
11 
12             var xhr;//声明一个全局变量
13             function create() {
14                 //创建XMLHttpRequest对象
15                 xhr = createXmlHttpRequest();
16                 //初始化XMLHttpRequest
17                 xhr.open("GET", "About.aspx", true);
18                 //设置浏览器不适用缓存
19                 xhr.sentRequestHeard("If-Modified-Since","0");
20                 //设置回调函数
21                 xhr.onreadystatechange = function () {
22                     //XMLHttpRequest状态属性
23                     if (xhr.readyState == 4) {
24                         if (status == 200) {
25                             alert(xhr.responseText); 
26                         } else {
27                             alert("系统繁忙,请联系管理员!");
28                         }
29                     }
30                 };
31                 xhr.send(null);
32 
33             }
34 
35         </script>

 

 

$.ajax({
    type:"POST";
    ContentType:"application/x-www-form-urlencoded";
    url:"Login.aspx";
    success:function(e){
                 var suggests=$("string",e.documentElement);
   error:function(e){
                alert("请求出现错误"+e.toString()); 
    }
    }
}
)

 

 

 

posted on 2012-11-19 21:02  韦爵爷++  阅读(242)  评论(0)    收藏  举报

导航