function jsonp({url, params, cb}) { return new Promise((resolve, reject) => { window[cb] = function (data) { // 声明全局变量 resolve(data) document.body.removeChild(script) } params = {...params, cb} let arrs = [] for(let key in params) { arrs.push(`${key}=${params[key]}`) } let script = document.createElement('script') script.src = `${url}?${arrs.join('&')}` document.body.appendChild(script) }) }
jsonp的缺点:
- 只能发送Get请求 不支持post put delete
- 不安全 xss攻击