function jsonp(options) {
options = options || {};
if (!options.url) {
throw new Error("参数url不能为空");
}
//是否缓存
if (options.cache && options.cache == false) {
options.data._v = Math.random();
}
var oHead = document.getElementsByTagName('head')[0];
var params = formatParams(options.data);
var oS = document.createElement('script');
oHead.appendChild(oS);
//回调函数
window[options.callback] = function(json) {
oHead.removeChild(oS);
clearTimeout(oS.timer);
window[options.callback] = null;
options.success && options.success(json);
};
oS.src = options.url + '?' + params;
//超时处理
if (options.time) {
oS.timer = setTimeout(function() {
window[options.callback] = null;
oHead.removeChild(oS);
原生JS实现Jsonp
最新推荐文章于 2024-06-21 00:21:30 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)