forked from Darylxyx/JavaScript-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
114 lines (94 loc) · 2.9 KB
/
server.js
File metadata and controls
114 lines (94 loc) · 2.9 KB
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
;(function(window, document, undefined) {
function Ajax(opts) {
opts.type = opts.type || 'get';
opts.type = opts.type.toLowerCase();
opts.dataType = opts.dataType || 'json';
opts.dataType = opts.dataType.toLowerCase();
if (opts.dataType == 'jsonp') {
jsonpRequest(opts);
return;
}
var xhr = new XMLHttpRequest(),
params = null;
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
opts.done && opts.done(jsonParse(xhr.responseText), xhr.responseXML);
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
opts.success && opts.success(jsonParse(xhr.responseText), xhr.responseXML);
} else {
opts.fail && opts.fail(jsonParse(xhr.responseText), xhr.responseXML);
}
}
}
if (opts.type == 'get') {
params = formatParams(opts.data);
var url = opts.url.indexOf('?') > -1 ? (opts.url + '&' + params) : (opts.url + '?' + params);
xhr.open('get', url, true);
opts.headers && setHeaders();
xhr.send(null);
} else if (opts.type == 'post') {
params = formatParams(opts.data);
xhr.open('post', opts.url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
opts.headers && setHeaders();
xhr.send(params);
}
function formatParams(data) {
var arr = [];
for (var key in data) {
arr.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
}
return arr.join('&');
}
function jsonpRequest(opts) {
if (!opts.url) {
console.error('url missing');
return;
}
opts.jsonpCallback = opts.jsonpCallback || 'callback';
var callbackName = 'jsonp_' + Math.ceil((Math.random() * 1E12));
opts.data[opts.jsonpCallback] = callbackName;
//创建script标签
var params = formatParams(opts.data),
oHead = document.querySelector('head'),
oScript = document.createElement('script');
oHead.appendChild(oScript);
//创建回调函数
window[callbackName] = function(json) {
oHead.removeChild(oScript);
window[callbackName] = null;
window.clearTimeout(oScript.timer);
opts.success && opts.success(json);
opts.done && opts.done(json);
};
//发起请求
oScript.src = opts.url + '?' + params;
if (opts.time) {
oScript.timer = window.setTimeout(function() {
oHead.removeChild(oScript);
window[callbackName] = null;
opts.fail && opts.fail({message: 'timeout'});
opts.done && opts.done({message: 'timeout'});
}, opts.time);
}
}
function setHeaders() {
for (var o in opts.headers) {
// console.log(o, opts.headers[o]);
xhr.setRequestHeader(o, opts.headers[o]);
}
}
function jsonParse(text) {
return JSON.parse(text);
}
}
window.Ajax = Ajax;
})(window, document);
if (typeof module !== 'undefined') {
module.exports = window.Ajax;
} else if (typeof define === 'function' && define.amd) {
define([], function () {
'use strict';
return window.Ajax;
});
}