原生Ajax封装随笔

XMLHttpRequest 对象用于和服务器交换数据。我们使用 XMLHttpRequest 对象的 open() 和 send() 方法:

open(method,url,async)

method:请求的类型;GET 或 POST

url:文件在服务器上的位置

async:true(异步)或 false(同步)

 

send(string)

string:仅用于 POST 请求

注:如果需要像 HTML 表单那样 POST 数据,需设置 setRequestHeader() 来添加 HTTP 头,然后在 send() 方法中规定您希望发送的数据:XMLHttpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");示例如下代码58行所示。

 

 1 var Factory = {
 2     create: function() {
 3         return function() { this.init.apply(this, arguments); }
 4     }
 5 }
 6 
 7 var Ajax = Factory.create();
 8 
 9 Ajax.prototype = {
10     init: function (successCallback, failureCallback) {
11         this.xhr = this.createXMLHttpRequest();
12         var xhrTemp = this.xhr;
13         var successFunc = null;
14         var failFunc = null;
15 
16         if (successCallback != null && typeof successCallback == "function") {
17             successFunc = successCallback;
18         }
19 
20         if (failureCallback != null && typeof failureCallback == "function") {
21             failFunc = failureCallback;
22         }
23 
24         this.get.apply(this, arguments);
25         this.post.apply(this, arguments);
26 
27         this.xhr.onreadystatechange = function () {
28             if (xhrTemp.readyState == 4) {
29                 if (xhrTemp.status == 200) {
30                     if (successFunc != null) {
31                         successFunc(xhrTemp.responseText, xhrTemp.responseXML);
32                     }
33                 }
34                 else {
35                     if (failFunc != null) {
36                         failFunc(xhrTemp.status);
37                     }
38                 }
39             }
40         }
41     },
42     get: function (url, async) {
43         this.xhr.open("GET", url, async);
44         this.xhr.send();
45     },
46     createXMLHttpRequest: function () {
47         if (window.XMLHttpRequest) {
48             return new XMLHttpRequest();
49         }
50         else {
51             return new ActiveXObject("Microsoft.XMLHTTP");
52         }
53 
54         throw new Error("Ajax is not supported by the browser!");
55     },
56     post: function (url, data, async) {
57         this.xhr.open("POST", url, async);
58         this.xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
59         this.xhr.send(data);
60     },
61     random: function (length) {
62         var array = new Array("0", "1", "2", "3", "5", "6", "7", "8", "9");
63         var len = parseInt(length);
64         var key = "";
65 
66         for (var i = 0; i < len; i++) {
67             key += Math.floor(Math.random() * 10);
68         }
69 
70         return key;
71     }
72 }

 

对于GET与POST方法使用如下:

       function get() { 
            var ajax = new Ajax(success,fail);
            ajax.get("Scripts/Util.js", true);
        }

        function post() {
            var ajax = new Ajax(success, fail);
            ajax.post("AjaxService.asmx/GetArgs","name=jasen", true);
        }
         
        function success(responseText, responseXML) {
            alert("result:" + responseText);
        }

        function fail(status) {
            alert("status:" + status);
        }

 

以上仅为练习随笔,仅供参考....

posted @ 2013-10-19 01:49  jasen.kin  阅读(...)  评论(...编辑  收藏