Handling session time-outs in Datatables (with server-side datasource processing)?

I would fire back a JSON response containing some sort of error code. To process it, you need to define fnServerData as you have surmised. However, I would strongly consider the use case before using the error callback for this reason.

I would fire back a JSON response containing some sort of error code. To process it, you need to define fnServerData as you have surmised. However, I would strongly consider the use case before using the error callback for this reason: Error is simply any problem fetching the resource, and uses status codes.

Let's say the session is terminated on the server. User requests the data, the server sends back a 500 error. Error callback says, "Well, that sucked.

Let's redirect to a login page. " Everything works great. However... the user makes a request.

The request is taking a little bit of time for whatever reason (large data set, network conditions). In the meantime, the user decides to navigate to another page, interrupting the call-response loop. With no response, the error callback is tripped and the user is redirected to a login page (or whatever function is set in the error callback).

The problem is that there's no status code that I'm aware of (I'd love to know, though! I don't mind being wrong here! ) for 'session has expired' in order to capture and process.

I'm not saying you "shouldn't" or "can't" use the error callback. But the function has to take into account errors other than the session expiring. You might need to get into processing differently based on status codes.

If your function handles all those cases elegantly, great! In one application, we are indeed redirecting to login page, and the error callback would often trip due to a false positive, dumping the user incorrectly to the login page. For the case of "session expired" we are catching it in the success callback via a JSON message.

Updated after Dave's excellent comments: If you have your server return a useful server error (401, 403, 550 or whatever makes sense in your scenario) then using fnServerData with statusCode parameter in the .ajax() call (that's a mouthful! ) would work just as well. I think it's the same amount of work: return JSON via a method you've already written, or return status error via methods you should already have access to.

Choose which one makes sense for you.

Just discovered the statusCode parameter in . Ajax, which may help with that side of things. I would still use the success handler for expired sessions, and statusCode or error for other problems.

– Greg Pettit Oct 5 at 19:20 2 If the user navigates away from the page the Ajax request would go away and no callback will fire, no? Whether the server sends back a successy or errory HTTP code is a matter of preference and domain. IMO requests that require a session but don't have one should return a "forbidden" code, since it's the truth.

– Dave Newton Oct 5 at 20:32 You would think. I'm not sure what magic is involved, but the callback indeed fires if you navigate away in the middle of a request. A forbidden code is a good idea, though.

Handling it with statusCode parameter would be easy, too. If your application doesn't need to distinguish between session expiry and other forbidden states, I'm leaning towards that as a better solution. – Greg Pettit Oct 5 at 20:59 My fault, I use beforeUnloads as per stackoverflow.Com/questions/1906023/… and haven't really thought about it in a long time; now I feel silly :) – Dave Newton Oct 5 at 21:05 Thanks gentlmen, that's what I ended up doing exactly: return a well formed JSON back when session times out on the server side with an error message in it, parsing it on the fnServeData on the client side – Ashkan Aryan Oct 57 at 9:32.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions