I'm using jquery jqGrid plugin in my java spring project. I'm using server side paging and sorting, so when user changes the page, jqgrid sends an ajax request. My problem is, sometimes the user's session may has been timed out during ajax request and my server side code, redirect the ajax request to login page. I need to detect server redirection in the jqgrid ajax requests. In fact, I want an afterRequest event that fires after every jqgrid requests. I see loadComplete
and beforeProcessing
events, but these fire after requests which have data and if the request does not have data or redirects on server, these events does not fire. How can I do this?

- 7,939
- 15
- 60
- 114
2 Answers
I think that you have to change your requirement. The redirection works on transparent level and it's the part of specification of XMLHttpRequest
(see here and the old answer). What you probably need is setting the headers required by the server side (like setting xhrFields: { withCredentials: true }
options of jQuery.ajax
via ajaxGridOptions: { xhrFields: { withCredentials: true } }
). If it would be required you can specify any other specific value of Access-Control-Allow-Origin
HTTP header using loadBeforeSend
callback. It has jqXHR
object as the parameter, which is extension of XMLHTTPRequest
object. Thus you can use jqXHR.setRequestHeader
method for example (see the answer) for practically customization of the request.
If the final response after redirection have another body then you can detect it inside of either beforeProcessing
or loadError
callback. Both callbacks contains jqXHR` object as the parameter. Thus you have full access to all information from the underlying Ajax response.
Sorry, that I can't provides you more details information, but your questions don't contains not enough technical details (HTTP trace for example) about the problem.
you could handle the ajax redirection for any jqgrid or others components with a globa ajax management as in this script :
$(document)
.ajaxStart(function() { $('#loadingPane').show(); }) /* catch ajax start event*/
.ajaxStop(function() { $('#loadingPane').hide(); }) /* catch ajax stop event */
.ajaxError(function(event, jqxhr, settings, exception) /* catch ajaxerror and specifically redirections */
{
if ( settings.url.indexOf("aspx")>0 && (jqxhr.status === 401 || jqxhr.status === 403) ) {
event.stopPropagation();
location.reload(true);
}
}
);

- 563
- 1
- 5
- 15