A
A
Alexander Vinogradov2017-02-18 00:31:31
Django
Alexander Vinogradov, 2017-02-18 00:31:31

How to determine in django that an ajax request is an XMLHttpRequest?

How to determine in a view that the request came using ajax, or rather, through javascript XMLHttpRequest or fetch?
Since I use javascript, the if request.is_ajax() check in django does not work, because false.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Vinogradov, 2017-02-18
@ruchej

Since the ajax request was made on javascript, and not on jQuery, it was necessary to manually add the necessary attribute to the request header so that django would define request.is_ajax()

fetch('http://127.0.0.1:8000/main/katalog/5/', {
              method: 'GET',
              headers: {'X-Requested-With': 'XMLHttpRequest'}});

P
Peter, 2017-02-18
@petermzg

Request Header must contain
X-Requested-With:XMLHttpRequest

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question