-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
130 lines (115 loc) · 3.52 KB
/
plugin.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
+ function () {
'use strict'
var JinAjax = function (obj) {
this.obj = obj
this.init()
}
JinAjax.prototype.getParam = function () { //参数解析
this.obj = this.obj || {}
this.obj.methods = this.obj.methods || 'GET' //undefine 情况过滤
this.obj.methods = this.obj.methods.toUpperCase()
this.obj.url = this.obj.url || ''
this.obj.data = this.obj.data || null
this.obj.header = this.obj.header || null
this.obj.timeout = this.obj.timeout || 10000
this.obj.type = this.obj.type || 'json'
this.obj.jsonp = this.obj.jsonp || 'callback'
this.obj.success = this.obj.success || function (res) {
console.log(res)
}
this.obj.error = this.obj.error || function (err) {
console.log(err)
}
this.obj.before = this.obj.before || function () {}
this.flag = ''
this.timeoutBool = false //是否超时
}
JinAjax.prototype.getXHR = function () { //兼容性检查
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else {
var versions = ["Microsoft", "msxm3", "msxml2", "msxml1"];
for (var i = 0; i < versions.length; i++) {
try {
var version = versions[i] + ".XMLHTTP";
return new ActiveXObject(version);
} catch (e) {
console.log('error ajax', e)
}
}
}
}
JinAjax.prototype.timeOutSet = function (xhr,dom) { //设置超时定时器
var _this = this
var timeId = setTimeout(function () {
_this.obj.error('request timeout more than ' + _this.obj.timeout + 'ms')
_this.timeoutBool = true
if (_this.obj.type === 'jsonp') {
document.body.removeChild(dom);
} else {
xhr.abort()
}
}, _this.obj.timeout)
this.flag = timeId
}
JinAjax.prototype.setHeader = function (xhr) { //header设置
for (var i in this.obj.header) {
xhr.setRequestHeader(i, this.obj.header[i]);
}
}
JinAjax.prototype.createXHR = function () { //创建xhr请求
var _this = this
var xhr = this.getXHR()
xhr.open(this.obj.methods, this.obj.url, this.obj.async)
this.timeOutSet(xhr, 0)
this.setHeader(xhr)
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (_this.timeoutBool) {
return;
}
clearTimeout(_this.flag)
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
var responseText = xhr.responseText;
try {
xhr.responseText && (responseText = JSON.parse(responseText));
obj.success(responseText);
} catch (e) {
obj.error(xhr);
}
} else {
obj.error(xhr);
}
}
}
xhr.send(obj.methods === "GET" ? null : obj.data);
}
JinAjax.prototype.setJsonp = function () { //JSONP构建
var _this = this
var script = document.createElement('script')
window.callback = function (data) {
document.body.removeChild(script)
clearTimeout(_this.flag)
try {
data
} catch (e) {
_this.obj.error(e)
}
_this.obj.success(data);
}
script.src = this.obj.url + (this.obj.url.indexOf("?") > -1 ? "" : "?") + _this.obj.jsonp +"=callback"
script.type = "text/javascript";
document.body.appendChild(script);
this.timeOutSet(0, script);
}
JinAjax.prototype.init = function () {
this.getParam()
this.obj.before()
if (this.obj.type === 'jsonp') {
this.setJsonp()
} else {
this.createXHR()
}
}
window.JinAjax = JinAjax //注册全局对象
}()