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
This commit is contained in:
Malcolm Tredinnick 2008-03-20 07:16:16 +00:00
parent c01e6b852a
commit c0537a961f
3 changed files with 21 additions and 0 deletions

View File

@ -245,6 +245,7 @@ answer newbie questions, and generally made Django that much better:
michael.mcewan@gmail.com michael.mcewan@gmail.com
michal@plovarna.cz michal@plovarna.cz
Mikko Hellsing <mikko@sorl.net> Mikko Hellsing <mikko@sorl.net>
Daniel Lindsley <polarcowz@gmail.com>
Orestis Markou <orestis@orestis.gr> Orestis Markou <orestis@orestis.gr>
Slawek Mikula <slawek dot mikula at gmail dot com> Slawek Mikula <slawek dot mikula at gmail dot com>
mitakummaa@gmail.com mitakummaa@gmail.com

View File

@ -82,6 +82,9 @@ class HttpRequest(object):
def is_secure(self): def is_secure(self):
return os.environ.get("HTTPS") == "on" return os.environ.get("HTTPS") == "on"
def is_ajax(self):
return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
def _set_encoding(self, val): def _set_encoding(self, val):
""" """
Sets the encoding used for GET/POST accesses. If the GET or POST Sets the encoding used for GET/POST accesses. If the GET or POST

View File

@ -199,6 +199,23 @@ Methods
Returns ``True`` if the request is secure; that is, if it was made with Returns ``True`` if the request is secure; that is, if it was made with
HTTPS. 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 QueryDict objects
----------------- -----------------