-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.js
100 lines (87 loc) · 2.92 KB
/
ajax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
;!function (global) {
// 导出方法
function _export (name, handle) {
if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = handle;
} else if (typeof define === 'function' && define.amd) {
define([], function () {
return handle;
});
} else {
global[name] = handle;
}
}
function param(g){function f(c,a){a="function"===typeof a?a():a;a=void 0===a||null===a||"undefined"===typeof a?"":a;e[e.length]=encodeURIComponent(c)+"="+encodeURIComponent(a)}function d(c,a){var b;if(c)switch(getType(a)){case "Array":for(b=0;b<a.length;b++)d(c+"["+("object"===typeof a[b]&&a[b]?b:"")+"]",a[b]);break;case "Object":for(b in a)d(c+"["+b+"]",a[b]);break;default:f(c,a)}else switch(getType(a)){case "String":case "Object":for(b in a)d(b,a[b]);break;case "Array":for(b=0;b<a.length;b++)f(a[b].name,
a[b].value)}}var e=[];d("",g);return e.join("&")};
function tempFunc () {}
function getType (data) {
return Object.prototype.toString.call(data).split(' ')[1].slice(0, -1)
}
function _Function (data) {
return getType(data)==='Function' ? data : tempFunc
}
function ajax (opt) {
var url = opt.url
var type = opt.type || 'GET'
var processData = opt.processData === false ? false : true; // 是否转换数据
var data = processData ? param(opt.data) : opt.data;
var success = _Function(opt.success)
var error = _Function(opt.error)
var timeout = 1000 * 15;
if(getType(opt.timeout) === 'Number') {
timeout = opt.timeout < 20 ? 20 : opt.timeout
}
// GET方式拼接url并清空data
switch (type) {
case 'GET':
url += (/\?/g.test(url) ? '&' : '?') + data;
url += (data ? '&' : '') + 'ajax_time=' + +new Date()
data = null
break;
}
/********** ajax start **************/
var xhr, timer;
try {
xhr = new XMLHttpRequest();
} catch (e) {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
xhr.open(type, url, true);
if(opt.contentType!==false) {
if(opt.contentType) {
xhr.setRequestHeader('content-type', opt.contentType);
} else {
xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
}
}
xhr.onreadystatechange = function() {
if ( xhr.readyState == 4 ) {
if ( xhr.status == 200 ) {
var res = xhr.responseText
try { res = JSON.parse(res) } catch (e) {}
success(res);
} else {
error(xhr);
}
}
}
xhr.send(data);
// 处理超时
timer = setTimeout(function () {
cancel('time out')
}, timeout);
/********** ajax end **************/
// 取消请求
function cancel (msg) {
xhr.msg = msg
xhr.abort()
}
return {
// 终止请求
abort: function () {
clearTimeout(timer)
}
}
};
_export('ajax', ajax)
} (this);