添加 JSONP 函数

This commit is contained in:
2021-07-13 00:15:10 +08:00
parent d8f48d4ce0
commit 37c85d31ae

View File

@@ -17,6 +17,32 @@ class Utils {
}
}
/**
* JSONP
*
* @param {object} options 配置选项
*/
jsonp(options) {
var config = {
url: '',
cbName: 'cb',
cbFunc: function (data) {
console.log('[JSONP]', data);
},
};
var scriptElem = document.createElement('script');
Object.assign(config, options);
window[config.cbName] = config.cbFunc;
scriptElem.addEventListener('load', function () {
this.remove();
});
scriptElem.setAttribute('src', config.url);
document.body.appendChild(scriptElem);
}
}
const utils = new Utils;