前端有没有统一跳转的工具

开发环境:
前端react
后端java

原来是在返回值中看code是多少,来决定是否跳转到登录界面。只在某几个接口设置了跳转。
现在未登录状态下,后端的接口都会返回401.
有没有什么工具,能统一设置。对于所有的接口,如果请求接口返回状态是401就跳转到登录页面去?


慕尼黑5688855
浏览 809回答 2
2回答

繁花不似锦

// 拦截ajax请求,检测是否超时,以重新登录$(document).ajaxComplete((event, xhr, settings) => {&nbsp; &nbsp; if (xhr.status === 200) {&nbsp; &nbsp; &nbsp; &nbsp; if (settings.dataType === 'json' && xhr.responseJSON !== void 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let result = xhr.responseJSON;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (2001 === result.code) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 没有session登录信息时跳转至登录页&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; global.location.href = "/main-login.html";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else if (xhr.status === 401) {} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;global.location.href = "/main-login.html";&nbsp; &nbsp; }});export default function(options) {&nbsp; &nbsp; const defaultOptions = {&nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; cache: true,&nbsp; &nbsp; &nbsp; &nbsp; jsonp: 'callback'&nbsp; &nbsp; };&nbsp; &nbsp; options.data = processRequest(options);&nbsp; &nbsp; //url这里加一些代理路径。。。&nbsp; &nbsp; options.url = options.url;&nbsp; &nbsp; options.headers = {&nbsp; &nbsp; &nbsp; &nbsp; "Accept": "application/json",&nbsp; &nbsp; &nbsp; &nbsp; "Content-Type": "application/json"&nbsp; &nbsp; };&nbsp; &nbsp; return $.ajax({...defaultOptions, ...options }).then(processResponse);};// 标准化传给后台的参数function processRequest(r) {&nbsp; &nbsp; const str = r.data || {};&nbsp; &nbsp; if ('get' == r.method) {&nbsp; &nbsp; &nbsp; &nbsp; if ($.isEmptyObject(str) || null == str) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t: new Date().getTime()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //添加时间戳随机数&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params: JSON.stringify(str),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t: new Date().getTime()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return JSON.stringify(str);&nbsp; &nbsp; }}// 标准化后台相应数据格式function processResponse(r) {&nbsp; &nbsp; let str = {};&nbsp; &nbsp; if (r.rows) {&nbsp; &nbsp; &nbsp; &nbsp; str = r;&nbsp; &nbsp; &nbsp; &nbsp; str.code = 0;&nbsp; &nbsp; &nbsp; &nbsp; str.list = r.rows;&nbsp; &nbsp; &nbsp; &nbsp; delete str.rows;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; if (!r.error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (0 <= r.code) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str = r;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str.code = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str.data = r;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str.code = -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str.message = r.message || r.error;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return str;}

紫衣仙女

难道你的每个接口都直接调用了XMLHttpRequest吗?建议将XMLHttpRequest封装成ajax方法提供给调用接口使用,在这个ajax方法中对返回的body json的状态码进行判断及跳转
打开App,查看更多内容
随时随地看视频慕课网APP