Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'?

like image 937
Yarin Avatar asked Dec 14 '11 17:12

Yarin


People also ask

How do I know if request is AJAX request?

In Laravel, we can use $request->ajax() method to check request is ajax or not.

What is AJAX in Python?

AJAX allows web pages to be updated asynchronously by exchanging data with a web server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.

Does AJAX use Python?

Another way of using Ajax in Django is to use the Django Ajax framework. The most commonly used is django-dajax which is a powerful tool to easily and super-quickly develop asynchronous presentation logic in web applications, using Python and almost no JavaScript source code.


1 Answers

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.

like image 184
Martin Geisler Avatar answered Oct 02 '22 23:10

Martin Geisler