原生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方法使用如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 @   jasen.kin  阅读(3841)  评论(6编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示