参考答案:
1const jsonp = ({ url, params, callbackName }) => { 2 const generateUrl = () => { 3 let dataSrc = ''; 4 for (let key in params) { 5 if (Object.prototype.hasOwnProperty.call(params, key)) { 6 dataSrc += `${key}=${params[key]}&`; 7 } 8 } 9 dataSrc += `callback=${callbackName}`; 10 return `${url}?${dataSrc}`; 11 } 12 return new Promise((resolve, reject) => { 13 const scriptEle = document.createElement('script'); 14 scriptEle.src = generateUrl(); 15 document.body.appendChild(scriptEle); 16 window[callbackName] = data => { 17 resolve(data); 18 document.removeChild(scriptEle); 19 } 20 }) 21} 22
最近更新时间:2021-07-07