From c0537a961fcf412ff284a838e4394fc537c14dcf Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Thu, 20 Mar 2008 07:16:16 +0000 Subject: [PATCH] Fixed #6616 -- Added an is_ajax() method to HttpRequest that uses the de facto standard header for detecting an XmlHttpRequest call. Thanks, Daniel Lindsley. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7334 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/http/__init__.py | 3 +++ docs/request_response.txt | 17 +++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/AUTHORS b/AUTHORS index 947b750289..2329c8b86b 100644 --- a/AUTHORS +++ b/AUTHORS @@ -245,6 +245,7 @@ answer newbie questions, and generally made Django that much better: michael.mcewan@gmail.com michal@plovarna.cz Mikko Hellsing + Daniel Lindsley Orestis Markou Slawek Mikula mitakummaa@gmail.com diff --git a/django/http/__init__.py b/django/http/__init__.py index 5439aa6c63..7faa3c875e 100644 --- a/django/http/__init__.py +++ b/django/http/__init__.py @@ -82,6 +82,9 @@ class HttpRequest(object): def is_secure(self): return os.environ.get("HTTPS") == "on" + def is_ajax(self): + return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest' + def _set_encoding(self, val): """ Sets the encoding used for GET/POST accesses. If the GET or POST diff --git a/docs/request_response.txt b/docs/request_response.txt index 4dcdf10adf..0e0f046a2d 100644 --- a/docs/request_response.txt +++ b/docs/request_response.txt @@ -199,6 +199,23 @@ Methods Returns ``True`` if the request is secure; that is, if it was made with HTTPS. +``is_ajax()`` + **New in Django development version** + + Returns ``True`` if the request was made via an XMLHttpRequest by checking + the ``HTTP_X_REQUESTED_WITH`` header for the string *'XMLHttpRequest'*. The + following major Javascript libraries all send this header: + + * jQuery + * Dojo + * MochiKit + * MooTools + * Prototype + * YUI + + If you write your own XMLHttpRequest call (on the browser side), you will + have to set this header manually to use this method. + QueryDict objects -----------------