公共js:
function urlLink(str){
var url = {
home:{
method:'POST',
url:'api.home/home'
},
}
}
var host='http://api/';//域名地址在
//请求方法 同步
function ajaxMethod(str,data) {
//获取哪个数据接口 str为上面url的key
var link = urlLink(str);
var result = {};
var token =localStorage.getItem('token');
$.ajax({
type: link.method,
url:host + link.url,
headers:{
token:token
},
data:JSON.stringify(data),
// dataType:'application/json',
async:false,
cache:true,
success:function (res) {
result = res;
}
});
return result;
};
//请求方法 异步
function ajaxMethodAsync(str,data,cb) {
//获取哪个数据接口 str为上面url的key
var link = urlLink(str);
var token =localStorage.getItem('token');
$.ajax({
type: link.method,
url:host + link.url,
headers:{
token:token
},
data:JSON.stringify(data),
dataType:'json',
// contentType:'application/json',
async:true,
cache:true,
success:function (res) {
return cb(null,res);
},
error: function(err){
return cb(err);
}
});
};
页面js
var data=[];//可字符串数组对象
//同步
var data=ajaxMethod('home',data);
if(data.code!=1000){
errorcode(data.code); //错误提示
}else{
//请求成功操作
}
//异步
ajaxMethodAsync('home',data,function(err,data){
if(err){
Toast('请检查网络连接是否正常',2000);//请求失败
}else{
if(data.code==1000){
//请求成功操作
}else{
errorcode(data.code);
}
}
});
1631

被折叠的 条评论
为什么被折叠?



