在npm上下载jsonp的包
这个包的用法 传入的参数是地址加上地址参数的混合 但是想封装一个将地址和地址参数分别传入的jsonp方法
所以来封装一个
import originJSONP from 'jsonp' export default function jsonp(url,data,options){ url+=(url.indexOf('?')<0?'?':'&')+param(data) return new Promise((resolve,reject)=>{ originJSONP(url,options,(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):'' }