2016-10-30 13:11:56
Introduction
Recent projects have found that Ajax requests cannot be redirected directly to the login page in the server. I looked up some information and found out that jquery's Ajax request was given a way. But there are some differences between Webix Ajax requests and jquery. Here, we imitate the processing of jquery to implement the Webix Ajax request session timeout jump.
Specific practices:
1, view webix.js source Discovery Webix.ajax only request before the listener function "Onbeforeajax", to do get return status Jump login page must have a return listener function, but the source does not. So I modified the next source, add a return of the listener function "Onafterajax".
The Red Flag section is the code I added, and automatically executes "Onafterajax" when it detects that Ajax is complete. (The location of the code can search webix.js, the condition "Onbeforeajax", and then add the red code in the corresponding location.)
if(Webix.callevent ("Onbeforeajax", [S, T, E, A, O,NULL, R])) { varH =!1; if("GET"!==s) {varL =!1; for(varCinchO) "content-type" = = C.tostring (). toLowerCase () && (L =! 0, "application/json" = = O[c] && (h =!0)); L|| (o["content-type"] = "application/x-www-form-urlencoded") } if("object" = =typeofEif(h) e = This. Stringify (e);Else { varU = []; for(varDinche) {varf =E[d]; (NULL= = = F | | f = = = webix.undefined) && (f = ""), "object" = =typeofF && (f = This. stringify (f)), U.push (d + "=" +encodeURIComponent (f))} E= U.join ("&")} e&& "GET" = = = S && (t = t + ( -1! = T.indexof ("?")? "&": "?") +E, E=NULL), A.open (S, T,! This. H); varb = This. Tw; b&& (A.responsetype =b); for(varCincho) A.setrequestheader (c, o[c]); varx = This; return This. master = This. master | | N, A.onreadystatechange =function () { if(!a.readystate | | 4 = =a.readystate) { If (webix.callevent ("Onafterajax", [a]) = = = = = =! 1) { return false
; }; if(webix.ajax.count++, I && x &&!)a.aborted) {if( -1! = Webix.ly.find (a))returnWebix.ly.remove (a); varT, E, s = X.master | | X, r = A.status >= 400 | | 0 = = =A.status; "blob" = = A.responsetype | | "Arraybuffer" = = A.responsetype? (t = "", E = a.response): (t = a.responsetext | | "", E =X.J (a)), Webix.ajax. $callback (S, I, T, E, A, R)} x&& (X.master =NULL), i = x = n =NULL } }, This. QH && (a.timeout = This. QH), This. H? A.send (E | |NULL): SetTimeout (function() {a.aborted|| ( -1! = Webix.ly.find (a)? Webix.ly.remove (a): A.send (E | |NULL)); }, 1), This. master && This. master. Ve && This. master. Ve.push (a), This. H?A:r}
2, WEBIX.AJX request no obvious sign, Jquery.ajax's logo is x-requested-with , so I simulated gave a logo requestflag= "Webix" (you can set a favorite), with " Onbeforeajax "Settings
Webix.attachevent ("Onbeforeajax",function(S, T, E, A, O) {o["Requestflag"]= "Webix"})
3. Monitor return status
Webix.attachevent ("Onafterajax",function(XHR) {if(Xhr.getresponseheader ("sessionstatus") = = ' Timeout ') {window.location.href= '/webix/login.html '}});
4. Backstage Code
4.1 Interceptor Code
PackageCom.ljx.filter;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.HandlerInterceptor;ImportOrg.springframework.web.servlet.ModelAndView; Public classUserinterceptorImplementsHandlerinterceptor {@Override Public voidaftercompletion (httpservletrequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throwsException {} @Override Public voidPosthandle (httpservletrequest arg0, httpservletresponse response, Object arg2, Modelandview arg3) throwsException {response.sendredirect ("/webix/login.html"); } @Override Public BooleanPrehandle (httpservletrequest request, httpservletresponse response, Object handler)throwsException {Object obj= Request.getsession (). getattribute ("LOGIN"); if(NULL= = obj) {//not logged in if(Request.getheader ("Requestflag")! =NULL&& Request.getheader ("Requestflag"). Equalsignorecase ("Webix")) {//if it is an AJAX request, the response header will have, RequestflagResponse.setheader ("Sessionstatus", "timeout");//setting the session status in the response header}Else{response.sendredirect (Request.getcontextpath ()+ "/login"); } return false; } return true; }}
4.2 Spring configuration file Join Interceptor Configuration
<mvc:interceptors> <Mvc:interceptor> <mvc:mappingPath= "/mvc/*" /> <Beanclass= "Com.ljx.filter.UserInterceptor"></Bean> </Mvc:interceptor> </mvc:interceptors>
4.3 Webix.ajax viewing effect under F12 console execution
Webix.ajax (). Get ("/webix/mvc/login.action")
WEBIX+SPRINGMVC Session Timeout Jump login page