jq在ajax请求前定义公用请求头

$.ajaxSetup({
	beforeSend: function(request) {

		var nonce = Date.parse(new Date());
		var appid = "PCA87hh7I8uu0ncs";
		var secret = "reL4tVMjA3HdFjsTkqnUmmZpBbqKgq";
		var Accept = "application/vnd.dtoa.v14+json";
		var url = localStorage.getItem("api_url");
		var user_token = localStorage.getItem("user_token");
		var Authorization = hex_md5(appid + "&" + secret + "&" + user_token + "&/" + url);
		console.log(user_token);
		var Authorization_java = hex_md5(user_token + nonce + url);
		Authorization = "Bearer " + user_token + "." + Authorization;
		request.setRequestHeader("nonce", nonce);
		request.setRequestHeader("Accept", Accept);
		request.setRequestHeader("user_token", user_token);
		request.setRequestHeader("Authorization", Authorization);
		request.setRequestHeader("Authorization_java", Authorization_java);
		request.setRequestHeader("APPID", appid);
		request.setRequestHeader("secret", secret);
		request.setRequestHeader("Client", "Pc");

		showLoading();
		
		if(user_token == null) {
			logoOut();
			$(".paster-con-container").remove()
		}
	},
	complete: function() {
		closeLoading();
	},
	error: function(data) {
		var data = data;
		console.log(data);
		if(data) {
			if(data.responseJSON) {
				if(data.responseJSON.status_code == 401) {
					if(!isAlert) {
						isAlert = true;
						alert(data.responseJSON.message);
						logoOut();
						$(".paster-con-container").remove()
					}
				} else {
					alert(data.responseJSON.message);
				}
			} else {
				console.log(JSON.stringify(data));
			}
		}
	}
});
$.ajaxSetup({
    beforeSend:function(request){
        var nonce = Date.parse(new Date());
        var Accept = "application/vnd.dtoa.v14+json";
        var url = "api/adviser/adviserGroups";
        var user_token = "4503776648acdfb9812ac8ad70990edc";
        var Authorization_java = hex_md5(user_token + nonce + url);
        request.setRequestHeader("nonce", nonce);
        request.setRequestHeader("Accept", Accept);
        request.setRequestHeader("user_token", user_token);
        request.setRequestHeader("Authorization_java", Authorization_java);
        request.setRequestHeader("APPID","PCA87hh7I8uu0ncs");
        request.setRequestHeader("secret","reL4tVMjA3HdFjsTkqnUmmZpBbqKgq");
        console.log(Authorization_java);
    },
    error:function(data){
		console.log(JSON.stringify(data));
    }
});

$.ajaxSetup ----  ajaxSetup() 方法为将来的 AJAX 请求设置默认值

语法:$.ajaxSetup({name:value, name:value, ... })

参数:

async布尔值,表示请求是否异步处理。默认是 true。

 

beforeSend(xhr)发送请求前运行的函数。可设置请求头

 

更多查看文档 http://www.runoob.com/jquery/ajax-ajaxsetup.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值