首先,我们下载实现jsonp的第三方库github地址:https://github.com/webmodules/jsonp
npm i --save-dev jsonp
import originJsonp from 'jsonp';
export default function jsonp(url,data,option) {
url += (url.indexOf('?')?'&':'?')+param(data);
return new Promise((resolve,reject)=>{
originJsonp(url,option,(err,data)=>{
if(!err){
resolve(data)
}else {
reject(err)
}
})
})
}
function param(data) {
let url = '';
for(var k in data){
let value = data[k] == undefined? '':data[k];
url += `&${k}=${encodeURIComponent(value)}`
}
return url ? url.substring(1):'';
}