87,987
社区成员
发帖
与我相关
我的任务
分享
前端JSON api mock方案http://jsonohyeah.com
提供数据文件在线创建,在线编辑,在线验证,私密保存,文件下载,API列表配置文件下载。
通过配置文件根据当前域名自动切换API对应的URL,完成零成本切换线上、预发、开发环境。
配置文件实例
!function (name, context, definition) {
if (typeof define == 'function') define(definition);
else if (typeof module != 'undefined') module.exports = definition();
else context[name] = definition();
}('apiBucket', this, function(){
var apiBucket = {
"check.js": {
"api": "my.app.getUserInfo",
"devUrl": "http://ac-pvq0flp7.clouddn.com/a79540d2621fb3b7.json",
"desc": "",
"url": "fill.this.by.yourself.please"
},
"f.this.team": {
"api": "my.app.setUser",
"devUrl": "http://ac-pvq0flp7.clouddn.com/7be1bbf7b5d936f.json",
"desc": "",
"url": "fill.this.by.yourself.please"
}
};
/*
* config product environment
* @param {string} host [server host name of your product environment]
* */
apiBucket.config = function (host) {
if ((window.location.host || window.location.hostname) != host) {
for (var api in apiBucket) {
var obj = apiBucket[api];
if (obj.hasOwnProperty('devUrl')) {
obj['url'] = obj['devUrl'];
delete obj['devUrl'];
}
}
}
}
return apiBucket;
})
//调用:
$.ajax({
url: apiBucket['my.app.getUserInfo'].url,
type: apiBucket['my.app.getUserInfo'].type,
data: {anyDataINeed: true},
success: function () {
console.log('Celebrate!');
}
});