Login

Workaround Firefox bug 553888

Author:
peroksid
Posted:
March 16, 2011
Language:
Python
Version:
Not specified
Score:
1 (after 1 ratings)

Firefox transparently follows redirects when AJAX calls return 3xx code. And it drops additional headers, X-Requested-With among them. Server treats redirected HTTP requested as non-AJAX. JS libraries has nothing to do here.

At 16.03.11 bug https://bugzilla.mozilla.org/show_bug.cgi?id=553888 has status "NEW" being reported at 21.03.10.

Workaround is following: - in process_response(): if request.is_ajax() and response has status_code 3xx then put response["Location"] to session, otherwise unset session stored value (if it is there). - in process_request(): if not request.is_ajax() and request.path equals to stored session value then monkeypatch request.is_ajax() return True (before any views come into play).

This results in smooth transparent redirects in Firefox, all treated as AJAX calls.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class FixFirefoxMissingAJAXRedirectHeadersMiddleware(object):
    key = 'FixFirefoxMissingAJAXRedirectHeadersMiddleware'
    
    def process_request(self, request):
        if not request.is_ajax() and request.path == request.session.get(self.key, None):
            setattr(request, 'is_ajax', lambda: True)
            
    def process_response(self, request, response):
        if request.is_ajax() and 300 <= response.status_code < 400:
            request.session[self.key] = response['Location']
        else:
            if self.key in request.session:
                del request.session[self.key]
        return response

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.