-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
63 lines (59 loc) · 1.56 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
import O from './tools/o';
/*
* url: string 请求地址
* callback: string 回调的方法名 默认 callback
* success: function 请求成功的回调
* error: function 请求失败的回调
* data: string 附加参数
* time: number 超时时间
*/
function jsonp(params) {
//创建script标签并加入到页面中
var callbackName = params.callback || 'callback';
var head = document.getElementsByTagName('head')[0];
// 设置传递给后台的回调参数名
var data = O.formatParams(params.data || {}, callbackName);
var script = document.createElement('script');
head.appendChild(script);
//创建jsonp回调函数
window[callbackName] = function(json) {
head.removeChild(script);
clearTimeout(script.timer);
window[callbackName] = null;
if (params.success) {
params.success(json);
}
};
//发送请求
script.src = params.url + '?' + data;
//为了得知此次请求是否成功,设置超时处理
if (params.time) {
script.timer = setTimeout(function() {
window[callbackName] = null;
head.removeChild(script);
if (params.error) {
params.error({
message: '超时',
});
}
}, params.time);
}
}
var VueJsonp = {
install: function(Vue) {
if (!Vue.prototype.$jsonp) {
Object.defineProperties(Vue.prototype, {
$jsonp: {
get: function() {
return VueJsonp;
},
},
});
}
},
jsonp: jsonp,
};
export default VueJsonp;
if(typeof window!=='undefined' && !window.$jsonp){
window.$jsonp = jsonp;
}