$.ajaxPrefilter(function (options, originalOptions, jqXHR) { //tm1、tm2表示开始时间、结束时间 var requestType, params, startTime, endTime; requestType = (originalOptions.type || "").toUpperCase(); //jsonp的时候type为undefined if (requestType === "GET") { params = originalOptions.data ? $.param(originalOptions.data) : originalOptions.url; } else if (requestType === "POST") { params = $.param(originalOptions.data); } if (params) { startTime = params.match(/(^tm1|&tm1)=([^&#]*)/i) ? decodeURIComponent(params.match(/(^tm1|&tm1)=([^&#]*)/i)[2].replace(/\+/g, "%20")) : null; endTime = params.match(/(^tm2|&tm2)=([^&#]*)/i) ? decodeURIComponent(params.match(/(^tm2|&tm2)=([^&#]*)/i)[2].replace(/\+/g, "%20")) : null; if (startTime && endTime) { startTime = startTime.replace(/\-/g, "/"); endTime = endTime.replace(/\-/g, "/"); if (new Date(startTime).getTime() > new Date(endTime).getTime()) { jqXHR.abort(); alert("开始时间不能大于结束时间"); } } } });
let requestSuccessCode = 200; // 表示请求成功 let tokenName = "accountToken"; $.ajaxSetup({ // ajax请求之前进行accountToken封装 beforeSend: function (xhr) { if(accountToken && accountToken != '') { xhr.setRequestHeader(tokenName, accountToken); } }, // ajax 请求完成返回结果 complete : function(request) { if(request.status == 200) { let responseCode = request.responseJSON.code; if(responseCode == 6001) { clearUserCookie(); let url = window.location.href; redirectLogin(url); } else if (responseCode == 6002) { window.open("/index.html"); } } }, // 表示请求错误 error :function(request){ console.info(request); } })
标签:jQuery,accountToken,originalOptions,tm1,ajaxPrefilter,params,startTime,参数,endTim From: https://www.cnblogs.com/saonian/p/16718933.html