JSONP Promise
XMit Lv3
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
/**
* @Author xmit
* @DateTime 2017-08-02
* @copyright [copyright]
* @license [license]
* @version [0.1.0]
* @param {[type]} url [jsonp link]
* @param {[type]} params [Object]
* @param {[type]} timeout [number]
* @return {[type]} [Promise]
* @example
*
* jsonp('https://www.ip.net/json/', {units:'si',lang:'zh'}, 5000)
*
*/
function jsonp(url, params, timeout) {
return new Promise(function(resolve, reject) {
let callbackname = '___JSONP_CALLBACK___' + Date.now() + Math.random().toString().substr(2)
let script = document.createElement('script')
script.src = url.indexOf('?') > -1 ? url + "&" + serialize(params) + '&callback=' + callbackname : url + '?' + serialize(params) + '&callback=' + callbackname
// 加载成功
window[callbackname] = function(data) {
clearTimeout(timerid)
removeScript()
resolve(data)
}
// 加载失败
script.onerror = function() {
reject(new Error('Script loading error.'))
clearTimeout(timerid)
removeScript()
}
// 加载超时
let timerid = setTimeout(function() {
reject(new Error('JSONP request timed out.'))
removeScript()
}, timeout)
document.body.append(script)

// 删除script标签
function removeScript() {
delete window[callbackname]
document.body.removeChild(script)
}
// 序列化
function serialize(params) {
let result = ""
for (let key in params) {
result += key + "=" + params[key] + '&'
}
return result.slice(0, result.length - 1)
}
})
}
由 Hexo 驱动 & 主题 Keep
本站由 提供部署服务