ERROR on document "request-response" , request.is_ajax()

120 views
Skip to first unread message

Vignesh Tnj

unread,
Jun 5, 2016, 7:52:56 AM6/5/16
to Django developers (Contributions to Django itself)
https://docs.djangoproject.com/en/1.9/ref/request-response/

HttpRequest.is_ajax()[source]

Returns True if the request was made via an XMLHttpRequest, by checking the HTTP_X_REQUESTED_WITH header for the string'XMLHttpRequest'. Most modern JavaScript libraries send this header. If you write your own XMLHttpRequest call (on the browser side), you’ll have to set this header manually if you want is_ajax() to work. "


if we set ,  xhttp.setRequestHeader('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');     as said in doc

request.is_ajax()   return False

-------------------------------

if we set , http.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

request.is_ajax()   return true

Paulo Gabriel Poiati

unread,
Jun 20, 2016, 9:32:46 AM6/20/16
to Django developers (Contributions to Django itself)
That is because the documentation is referring to the WSGI envrion dictionary (and thus the CGI spec). This is how the translation from the client to the server works: X-My-Header becomes HTTP_X_MY_HEADER. It isn't wrong IMO.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/7a88b8d8-b15f-4377-8264-e94130ad041e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
[]'s
Paulo Poiati

Tobias McNulty

unread,
Jun 20, 2016, 11:19:56 AM6/20/16
to django-developers
I think it's a fair point. The docs on HttpReqest.is_ajax() are specifically discussing headers set from the client side, so I think X-Requested-With would be more appropriate. This was discussed previously and a more substantial change proposed:

https://groups.google.com/forum/#!searchin/django-developers/HTTP_X_REQUESTED_WITH/django-developers/Jvs3F79cY4Y/GW-S9GTYOKsJ

In fact, Collin Anderson submitted a proof of concept PR to this ticket just ~2 days ago:


Vignesh, I would follow this ticket and see if your concern could be addressed through this implementation. If for whatever reason it doesn't go through, you might consider filing a ticket and pull request (if the ticket is accepted) for a small improvement to the documentation -- to change the header name referenced as you suggested and/or point people to the section of the docs that talks about how headers and META keys are different (https://docs.djangoproject.com/en/1.9/ref/request-response/#django.http.HttpRequest.META).

Cheers,
Tobias


For more options, visit https://groups.google.com/d/optout.



--

Tobias McNulty
Chief Executive Officer

tob...@caktusgroup.com
www.caktusgroup.com

Reply all
Reply to author
Forward
0 new messages