From 8672148eefd72ba3fae911c9c62d42d3dd4fffb3 Mon Sep 17 00:00:00 2001 From: sc Date: Sat, 18 Sep 2021 08:52:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0jsonp=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/request.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/utils/request.js b/src/utils/request.js index b7583fdb..70c9db1d 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -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) + }) } }