Search code examples
pythonajaxhttp-headersxmlhttprequest

Check if request is AJAX in Python


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

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


Solution

  • 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.