16
votes

Is there a way to check if a request is AJAX in Python?

The equivalent of PHP's $_SERVER['HTTP_X_REQUESTED_WITH'] == 'xmlhttprequest'?

3

3 Answers

33
votes

If the AJAX framework sets the X-Requested-With header in its requests, then you will be able to use that header to detect AJAX calls. It's up to the client-side framework to do this.

Getting hold of the HTTP headers depends on your Python framework of choice. In Django, the request object has an is_ajax method you can use directly.

0
votes

Check this how to check if request is ajax in turbogears

also this for how to do this in Django(it depends in your framework): How to check contents of incoming HTTP header request

0
votes

is_ajax() is deprecated since Django 3.1 (as of 28th May, 2021) as they stated that it depends on jQuery ways of sending request but since people use fetch method a lot to make call Ajax calls, it has become unreliable in many cases.

Usually, text/html is requested in the header in the first option in the browser when it just the loads the page. However, if you were making API call, then it would become */* by default, if you attach Accept: application/json in the headers, then it become

application/json

So, you can check easily if the request is coming from ajax by this

import re # make sure to import this module at the top

in your function

requested_html = re.search(r'^text/html', request.META.get('HTTP_ACCEPT'))
if not requested_html:
    # ajax request it is