X Tutup
Skip to content

Commit 6bcf941

Browse files
committed
update
1 parent 403ad74 commit 6bcf941

File tree

5 files changed

+93
-74
lines changed

5 files changed

+93
-74
lines changed

.DS_Store

0 Bytes
Binary file not shown.

cutter/.DS_Store

0 Bytes
Binary file not shown.

cutter/cutter/.DS_Store

6 KB
Binary file not shown.

jQuery-free/.DS_Store

0 Bytes
Binary file not shown.

jQuery-free/server.js

Lines changed: 93 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,113 @@
1-
function ajax(opts) {
2-
3-
opts.type = opts.type || 'get';
4-
opts.type = opts.type.toLowerCase();
5-
opts.dataType = opts.dataType || 'json';
6-
opts.dataType = opts.dataType.toLowerCase();
7-
8-
if (opts.dataType == 'jsonp') {
9-
jsonpRequest(opts);
10-
return;
11-
}
12-
13-
var xhr = new XMLHttpRequest(),
14-
params = null;
1+
;(function(window, document, undefined) {
2+
function Ajax(opts) {
153

16-
xhr.onreadystatechange = function() {
17-
if (xhr.readystate == 4) {
18-
opts.done && opts.done(xhr.responseText, xhr.responseXML);
19-
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
20-
opts.success && opts.success(xhr.responseText, xhr.responseXML);
21-
} else {
22-
opts.fail && opts.fail(xhr.responseText, xhr.responseXML);
23-
}
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;
2412
}
25-
}
2613

27-
if (opts.type == 'get') {
28-
params = formatParams(opts.data);
29-
xhr.open('get', opts.url + '?' + params, true);
30-
xhr.send(null);
31-
} else if (opts.type == 'post') {
32-
params = formDataParams(opts.data);
33-
xhr.open('post', opts.url, true);
34-
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoed');
35-
xhr.send(params);
36-
}
14+
var xhr = new XMLHttpRequest(),
15+
params = null;
3716

38-
function formatParams(data) {
39-
var arr = [];
40-
for (var key in data) {
41-
arr.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
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+
}
4226
}
43-
return arr.join('&');
44-
}
4527

46-
function formDataParams(data) {
47-
var fd = new FormData();
48-
for (var key in data) {
49-
fd.append(key, encodeURIComponent(data[key]));
28+
if (opts.type == 'get') {
29+
params = formatParams(opts.data);
30+
xhr.open('get', opts.url + '?' + params, true);
31+
// opts.headers && setHeaders();
32+
xhr.send(null);
33+
} else if (opts.type == 'post') {
34+
params = formatParams(opts.data);
35+
xhr.open('post', opts.url, true);
36+
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
37+
// opts.headers && setHeaders();
38+
xhr.send(params);
5039
}
51-
return fd;
52-
}
5340

54-
function jsonpRequest(opts) {
55-
56-
if (!opts.url) {
57-
console.error('url missing');
58-
return;
41+
function formatParams(data) {
42+
var arr = [];
43+
for (var key in data) {
44+
arr.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
45+
}
46+
return arr.join('&');
5947
}
6048

61-
opts.jsonpCallback = opts.jsonpCallback || 'callback';
49+
function jsonpRequest(opts) {
6250

63-
var callbackName = 'jsonp_' + (new Date()).getTime();
51+
if (!opts.url) {
52+
console.error('url missing');
53+
return;
54+
}
6455

65-
opts.data[opts.jsonpCallback] = callbackName;
56+
opts.jsonpCallback = opts.jsonpCallback || 'callback';
6657

67-
//创建script标签
68-
var params = formatParams(opts.data),
69-
oHead = document.querySelector('head'),
70-
oScript = document.createElement('script');
71-
oHead.appendChild(oScript);
58+
var callbackName = 'jsonp_' + (new Date()).getTime();
7259

73-
//创建回调函数
74-
window[callbackName] = function(json) {
75-
oHead.removeChild(oScript);
76-
window[callbackName] = null;
77-
window.clearTimeout(oScript.timer);
78-
opts.success && opts.success(json);
79-
opts.done && opts.done(json);
80-
};
60+
opts.data[opts.jsonpCallback] = callbackName;
8161

82-
//发起请求
83-
oScript.src = opts.url + '?' + params;
62+
//创建script标签
63+
var params = formatParams(opts.data),
64+
oHead = document.querySelector('head'),
65+
oScript = document.createElement('script');
66+
oHead.appendChild(oScript);
8467

85-
if (opts.time) {
86-
oScript.timer = window.setTimeout(function() {
68+
//创建回调函数
69+
window[callbackName] = function(json) {
8770
oHead.removeChild(oScript);
8871
window[callbackName] = null;
89-
opts.fail && opts.fail({message: 'timeout'});
90-
opts.done && opts.fail({message: 'timeout'});
91-
}, opts.time);
72+
window.clearTimeout(oScript.timer);
73+
opts.success && opts.success(json);
74+
opts.done && opts.done(json);
75+
};
76+
77+
//发起请求
78+
oScript.src = opts.url + '?' + params;
79+
80+
if (opts.time) {
81+
oScript.timer = window.setTimeout(function() {
82+
oHead.removeChild(oScript);
83+
window[callbackName] = null;
84+
opts.fail && opts.fail({message: 'timeout'});
85+
opts.done && opts.done({message: 'timeout'});
86+
}, opts.time);
87+
}
88+
}
89+
90+
function setHeaders() {
91+
for (var o in opts.headers) {
92+
console.log(o, opts.headers[o]);
93+
xhr.setRequestHeader(o, opts.headers[o]);
94+
}
95+
}
96+
97+
function jsonParse(text) {
98+
return JSON.parse(text);
9299
}
93100
}
94-
}
101+
102+
window.Ajax = Ajax;
103+
104+
})(window, document);
105+
106+
if (typeof module !== 'undefined') {
107+
module.exports = window.Ajax;
108+
} else if (typeof define === 'function' && define.amd) {
109+
define([], function () {
110+
'use strict';
111+
return window.Ajax;
112+
});
113+
}

0 commit comments

Comments
 (0)
X Tutup