JS JSONP 函数封装
转载自 https://my.oschina.net/shuaihong/blog/1542554
myJsonp = function(url,data,callback){ var fnName = 'myJsonp_' + Math.random().toString().replace('.',''); //定义一个全局回调函数 window[fnName] = callback; //初始化序列化参数 var querystring = ''; for(var attr in data){ querystring += attr + '=' + data[attr] + '&'; } //动态创建script标签 var script = document.createElement('script'); //后台接受回调函数,并调用 script.src = url + '?' + querystring + 'callback=' + fnName; //处理完毕之后,删除script标签,否则多次请求,页面会存在多个script标签 script.onload = function(){ document.body.removeChild(script); } document.body.appendChild(script); }