-
Notifications
You must be signed in to change notification settings - Fork 13
Redirection with AJAX
AJAX Pagination supports redirection out of the box. Normally, the behaviour of redirection through AJAX is quirky. On receiving a redirection response, a browser will "secretly" request the content at the new URL location. The javascript code will no nothing about the fact that it was redirected.
This has some implications for the AJAX content loaded. Firstly, AJAX Pagination adds to requests a parameter to the querystring so that the server-side code knows that it is an AJAX Pagination request. This would normally be lost in the redirection process. However, this would otherwise be easily solved, and is not a big problem.
The unfortunate behaviour of redirects (normally with a 302 status code), is that since the javascript code is not told of the redirection, the URL in the browser address bar will remain at the old URL address, and does not change to the new redirected URL address, as would occur if the request was a normal browser request.
To solve this problem, AJAX Pagination adds an after_filter to the ActionController class, which converts responses to AJAX Pagination requests which have a 302 status temporary redirect with a status code of 200 OK, which is normally used for a successful response of content. However, AJAX Pagination leaves the Location header in the response. That way, the client-side javascript will notice that a Location header is present, and correctly redirect to the new location via another AJAX request.
The implementation should not affect other redirect requests, because it is only active when the request is an AJAX Pagination request. Even other AJAX requests will not be affected.
There is one detail to ensure when redirecting. The new page will receive the AJAX request, along with the fact that it is an AJAX Pagination request. It needs code to handle the response. This is the same as any other response, and more detail is available at Responding to AJAX requests.
It is possible to disable the after_filter added to the ActionController. Simply generate an initializer and set config.redirect_after_filter = false. The after_filter can be added again only to specific controllers with:
after_filter :ajax_pagination_redirect