增加jsonp请求方式

This commit is contained in:
sc 2021-09-18 08:52:00 +08:00
parent 0c28eb2648
commit 8672148eef
1 changed files with 24 additions and 0 deletions

View File

@ -170,6 +170,30 @@ var http = {
reject(error);
})
})
},
/** jsonp
* @param {接口地址} url
* @param {JSONP回调函数名称} name
*/
jsonp: function(url, name='jsonp'){
return new Promise((resolve) => {
var script = document.createElement('script')
var _id = `jsonp${Math.ceil(Math.random() * 1000000)}`
script.id = _id
script.type = 'text/javascript'
script.src = url
window[name] =(response) => {
resolve(response)
document.getElementsByTagName('head')[0].removeChild(script)
try {
delete window[name];
}catch(e){
window[name] = undefined;
}
}
document.getElementsByTagName('head')[0].appendChild(script)
})
}
}