function jsonp (url,param,callback){
return new Promise((resolve,reject)=>{
let script = document.createElement('script');
window[callback] = function(data){
resolve(data);
document.removeChild('script');
};
let arr =[];
for(var key in param){
arr.push(`${key}=${param[key]}`);
}
script.type ='text/javascript';
script.src = `${url}?callback=${callback}&${arr.join('&')}`
document.body.appendChild(script);
})
}
jsonp({
url: 'http://suggest.taobao.com/sug',
callback: 'getData',
params: {
q: 'iphone手机',
code: 'utf-8'
},
}).then(data=>{console.log(data)})
// function $() {}
// $.jsonp = function(options) {
// let callbackName = 'jsonp'+Math.random().toString().replace('.', '');
// window[callbackName] = options.success;
// let params = '';
// for (let key in options.params) {
// params += '&'+key+'='+options.params[key];
// }
// let oScript = document.createElement('script');
// oScript.src = options.url+'?callback='+callbackName+params;
// document.body.appendChild(oScript);
// oScript.onload = function() {
// document.body.remove(oScript);
// }
// }
// $.jsonp({
// url: "https://wis.qq.com/weather/common",
// params: {
// source: 'pc',
// weather_type: "forecast_1h|forecast_24h|index|alarm|limit|tips",
// province: "湖北省",
// city: "孝感市",
// county: "安陆市"
// },
// success: function(data) {
// console.log(data);
// }
// })
console