X Tutup
Skip to content

Commit 62a6b3a

Browse files
committed
update
1 parent 34a63fa commit 62a6b3a

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

base/.DS_Store

6 KB
Binary file not shown.

base/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### Usage
2+
```
3+
//init
4+
<script src="./server.js"></script>
5+
or
6+
import Ajax from './server.js';
7+
8+
//use
9+
Ajax({
10+
data: data, //request data
11+
url: url, //request url
12+
dataType: 'jsonp', //jsonp or not
13+
type: 'get', //request method, support 'get' and 'post'
14+
headers: {
15+
header1: 'aaa',
16+
header2: 'bbb'
17+
},
18+
success: function(json) {
19+
console.log(json);
20+
},
21+
fail: function(json) {
22+
console.log(json);
23+
},
24+
done: function(json) {
25+
console.log(json);
26+
}
27+
});
28+
29+
```

base/base.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,31 @@
5252
        };
5353
};
5454

55+
Base.prototype.setCookie = function(name, value, days) {
56+
let Days = days || 1,
57+
exp = new Date();
58+
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
59+
document.cookie = name + "=" + escape (value) + ";expires=" + exp.toGMTString();
60+
};
61+
62+
Base.prototype.getCookie = function(name) {
63+
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); //正则匹配
64+
if (arr = document.cookie.match(reg)) {
65+
return unescape(arr[2]);
66+
} else {
67+
return null;
68+
}
69+
};
70+
71+
Base.prototype.delCookie = function(name) {
72+
var exp = new Date();
73+
exp.setTime(exp.getTime() - 1);
74+
var cval = this.getCookie(name);
75+
if (cval != null) {
76+
document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
77+
}
78+
};
79+
5580
var basefn = new Base();
5681

5782
window.basefn = basefn;

base/server.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
;(function(window, document, undefined) {
2+
function Ajax(opts) {
3+
4+
opts.type = opts.type || 'get';
5+
opts.type = opts.type.toLowerCase();
6+
opts.dataType = opts.dataType || 'json';
7+
opts.dataType = opts.dataType.toLowerCase();
8+
9+
if (opts.dataType == 'jsonp') {
10+
jsonpRequest(opts);
11+
return;
12+
}
13+
14+
var xhr = new XMLHttpRequest(),
15+
params = null;
16+
17+
xhr.onreadystatechange = function() {
18+
if (xhr.readyState == 4) {
19+
opts.done && opts.done(jsonParse(xhr.responseText), xhr.responseXML);
20+
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
21+
opts.success && opts.success(jsonParse(xhr.responseText), xhr.responseXML);
22+
} else {
23+
opts.fail && opts.fail(jsonParse(xhr.responseText), xhr.responseXML);
24+
}
25+
}
26+
}
27+
28+
if (opts.type == 'get') {
29+
params = formatParams(opts.data);
30+
var url = opts.url.indexOf('?') > -1 ? (opts.url + '&' + params) : (opts.url + '?' + params);
31+
xhr.open('get', url, true);
32+
opts.headers && setHeaders();
33+
xhr.send(null);
34+
} else if (opts.type == 'post') {
35+
params = formatParams(opts.data);
36+
xhr.open('post', opts.url, true);
37+
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
38+
opts.headers && setHeaders();
39+
xhr.send(params);
40+
}
41+
42+
function formatParams(data) {
43+
var arr = [];
44+
for (var key in data) {
45+
arr.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
46+
}
47+
return arr.join('&');
48+
}
49+
50+
function jsonpRequest(opts) {
51+
52+
if (!opts.url) {
53+
console.error('url missing');
54+
return;
55+
}
56+
57+
opts.jsonpCallback = opts.jsonpCallback || 'callback';
58+
59+
var callbackName = 'jsonp_' + Math.ceil((Math.random() * 1E12));
60+
61+
opts.data[opts.jsonpCallback] = callbackName;
62+
63+
//创建script标签
64+
var params = formatParams(opts.data),
65+
oHead = document.querySelector('head'),
66+
oScript = document.createElement('script');
67+
oHead.appendChild(oScript);
68+
69+
//创建回调函数
70+
window[callbackName] = function(json) {
71+
oHead.removeChild(oScript);
72+
window[callbackName] = null;
73+
window.clearTimeout(oScript.timer);
74+
opts.success && opts.success(json);
75+
opts.done && opts.done(json);
76+
};
77+
78+
//发起请求
79+
oScript.src = opts.url + '?' + params;
80+
81+
if (opts.time) {
82+
oScript.timer = window.setTimeout(function() {
83+
oHead.removeChild(oScript);
84+
window[callbackName] = null;
85+
opts.fail && opts.fail({message: 'timeout'});
86+
opts.done && opts.done({message: 'timeout'});
87+
}, opts.time);
88+
}
89+
}
90+
91+
function setHeaders() {
92+
for (var o in opts.headers) {
93+
// console.log(o, opts.headers[o]);
94+
xhr.setRequestHeader(o, opts.headers[o]);
95+
}
96+
}
97+
98+
function jsonParse(text) {
99+
return JSON.parse(text);
100+
}
101+
}
102+
103+
window.Ajax = Ajax;
104+
105+
})(window, document);
106+
107+
if (typeof module !== 'undefined') {
108+
module.exports = window.Ajax;
109+
} else if (typeof define === 'function' && define.amd) {
110+
define([], function () {
111+
'use strict';
112+
return window.Ajax;
113+
});
114+
}

0 commit comments

Comments
 (0)
X Tutup