-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
64 lines (56 loc) · 2.26 KB
/
index.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
;(function() {
var formatParams = function(data) {//格式化参数
var arr = [];
for (var name in data) {
arr.push(encodeURIComponent(name) + '=' + encodeURIComponent(data[name]));
}
return arr.join('&');
}
var jsonpPipe = function(options) {
options = options || {};
if (!options.url) return;
//创建 script 标签并加入到页面中
var url = options.url;
//缓存时回调名应该唯一
var urlCut = url.slice(url.length-10,url.length).replace(/=|&\//g,'').replace('?', '');//去掉截取后的url里面的特殊字符
var callbackName = options.cache ? ('jsonp_' + urlCut) : ('jsonp_' + Math.random()).replace(".", "");
var oBody = document.getElementsByTagName('body')[0];
var params = "";
options.callback = options.callback || 'callback';
if(options.data){
options.data[options.callback] = callbackName;
params += formatParams(options.data);
}else{
params+=options.callback+"="+callbackName;
}
var scriptTag = document.createElement('script');
oBody.appendChild(scriptTag);
//创建jsonp回调函数
window[callbackName] = function (json) {
oBody.removeChild(scriptTag);
clearTimeout(scriptTag.timer);
window[callbackName] = null;
options.success && options.success(json);
};
//发送请求
var symbol = options.url.indexOf('?') > 0 ? '&' : '?';
scriptTag.src = options.url + symbol + params;
//超时处理
if (options.time) {
scriptTag.timer = setTimeout(function () {
window[callbackName] = null;
oBody.removeChild(scriptTag);
options.fail && options.fail({ msg: "timeout" });
}, options.time);
}
};
if(typeof module !== 'undefined' && module.exports) {
module.exports = jsonpPipe;
} else if(typeof define === 'function' && (define.amd || define.cmd)) {
define(function() { return jsonpPipe; });
} else {
this.jsonpPipe = jsonpPipe;
}
}).call(function() {
return this || (typeof window != 'undefined' ? window : global);//作用域
}());