2 Star 6 Fork 2

ereddate/ptemplatejs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ptemplate.extend.jsonp.js 2.33 KB
一键复制 编辑 原始数据 按行查看 历史
ereddate 提交于 2018-12-05 09:43 . no commit message
/*!
* ptemplatejs v1.0.0
* @author yandong
*
* https://github.com/ereddate/ptemplatejs
*/
'use strict';
typeof window.pTemplate != "undefined" && (function(win, $) {
$.jsonp = (url, data, ops) => {
if (url == "") return;
if (!data) data = "";
var complete = function(result, success, error) {
if (result && result.status === 1) {
success && success(result.data || result, result.msg || "success.", result.code || 1, result);
} else if (result && result.status === 0) {
error && error(result.msg || "unknown error.", result.code || 0);
} else {
success && success(result);
}
},
fail = function(error, msg) {
error && error(msg || "unknown error.", 0);
},
jsonp = function(success, error) {
var head = document.getElementsByTagName("head")[0],
callback = "ptemplate_jsonp_" + (Math.random(10000) + "").replace(".", "");
while (window[callback]) {
callback = "ptemplate_jsonp_" + (Math.random(10000) + "").replace(".", "");
}
window[callback] = function(data) {
window[callback] = null;
document.getElementById(callback).parentNode.removeChild(document.getElementById(callback));
try {
data = data || new Function('return ' + data)();
console.log(callback)
complete(data, success, error);
} catch (e) {
fail(error, e.message);
}
};
try {
var script = document.createElement("script");
head.appendChild(script);
script.timeout = setTimeout(function() {
if (window[callback] != null) {
window[callback] = null;
head.removeChild(document.getElementById(callback));
fail(error, "timeout " + callback);
}
}, ops && ops.timeout || 5000);
script.id = callback;
script.src = url + (/\?/.test(url) ? "&" : "?") + (ops && ops.callback || "callback") + "=" + (ops && ops.callbackName || callback);
script.onload = function(a) {
};
script.onerror = function(err) {
head.removeChild(this);
window[callback] = null;
fail(error, err || "request error");
};
} catch (e) {
fail(error, e.message);
}
};
return {
done: function(success, error) {
setTimeout(function() {
new jsonp(success, error);
}, 500);
return this;
}
}
}
})(window, pTemplate)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/ereddate2017/ptemplatejs.git
[email protected]:ereddate2017/ptemplatejs.git
ereddate2017
ptemplatejs
ptemplatejs
master

搜索帮助