Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django request.is_ajax returning false

Tags:

I am testing out ajax with Django + jQuery. I have this in my views:

def ajax_test(request):
    if request.is_ajax():
        message = "This is ajax"
    else:
        message = "Not ajax"
    return HttpResponse(message)

and this in my template:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="{{ STATIC_URL }}js/jquery-1.6.4.js"></script>
</head>
<body>
<script>
$.get("/ajax_test", function(data) {
    alert(data);
    });
});
</script>
</body>
</html>

My question is - why does this return "Not ajax" to my html page?

like image 417
Darwin Tech Avatar asked Dec 21 '11 09:12

Darwin Tech


3 Answers

I don't know what happened to you. I tested and got a good message: This is ajax. My code:

urls.py

url(r'^$', 'myapp.views.home', name='home'), url(r'^ajax_test/$', 'myapp.views.ajax_test', name='ajax_test'), 

views.py

def home(request):     return render_to_response('home.html', {},                           context_instance=RequestContext(request))  def ajax_test(request):     if request.is_ajax():         message = "This is ajax"     else:         message = "Not ajax"     return HttpResponse(message) 

templates/home.html

<!DOCTYPE html> <html> <head>     <title></title>     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> </head> <body> <script type="text/javascript">     $(document).ready(function () {         $.get("/ajax_test/", function (data) {             alert(data);         });     }); </script> </body> </html> 
like image 183
anhtran Avatar answered Sep 23 '22 15:09

anhtran


Looks like is_ajax just checks the HTTP_X_REQUESTED_WITH (looks like X-Requested-With in HTTP) header. If it equals XMLHttpRequest, we have an ajax query.

So it's pretty easy now to trace the fate of this header:

  1. Look at the initial HTTP request (using browser's debug plugin, Wireshark, tcpdump or any other tool you prefer) and see if it has a correct HTTP_X_REQUESTED_WITH header. If it doesn't, the problem is with jQuery (or your script).

  2. The header can be lost during redirection, as Daniel Roseman said (haven't seen this myself, but almost sure it's possible). You'll see two (or more) chained HTTP requests in this case, and one of them will lack the HTTP_X_REQUESTED_WITH header. In this case, the problem could be with the webserver's config or redirection itself (you may have to get rid of it or manually relay the header if redirection response is generated by your code).

  3. Also, HTTP proxies can cut headers. In this case you may look at the HTTP request on the server side via traffic inspector or request.environ (pretty garbled with environment vars, but HTTP headers are also there).

like image 25
ogurets Avatar answered Sep 22 '22 15:09

ogurets


Probably you've got APPEND_SLASH turned on (the default), and Django is redirecting from /ajax_test to /ajax_test/ but not preserving the relevant header. Better to request the version with the slash in the first place.

like image 41
Daniel Roseman Avatar answered Sep 23 '22 15:09

Daniel Roseman