ajax设置token的公共权限

window.base={

g_restUrl:‘http://www.dzm.com/’,
getData:function(params){
if(!params.type){
params.type=‘get’;
}
var that=this;
$.ajax({
type:params.type,
url:this.g_restUrl+params.url,
data:params.data,
beforeSend: function (XMLHttpRequest) {
if (params.tokenFlag) {
XMLHttpRequest.setRequestHeader(‘token’, that.getLocalStorage(‘token’));
}
},
success:function(res){
params.sCallback && params.sCallback(res);
},
error:function(res){

            params.eCallback && params.eCallback(res);
        }
    });
},
setLocalStorage:function(key,val){
    var exp=new Date().getTime()+2*24*60*60*100;  //令牌过期时间
    var obj={
        val:val,
        exp:exp
    };
    localStorage.setItem(key,JSON.stringify(obj));
},

getLocalStorage:function(key){
    var info= localStorage.getItem(key);
    if(info) {
        info = JSON.parse(info);
        if (info.exp > new Date().getTime()) {
            return info.val;
        }
        else{
            this.deleteLocalStorage('token');
        }
    }
    return '';
},
deleteLocalStorage:function(key){
    return localStorage.removeItem(key);
},
}

你可能感兴趣的:(token,个人笔记,jq)