Django request.is_ajax returning false

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>

Adjusted my code to the following:

views.py:

def index(request):
    return render_to_response('index.html',
                            locals(), context_instance=RequestContext(request)) 

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

urls.py:

urlpatterns = patterns('',   
    #    root
    url(r'^$', views.index, name='index'), 
    #    ajax
    url(r'^ajax_test/$', views.ajax_test, name='ajax_test'),
) 

template:

 ...
 <script type="text/javascript">
 $(document).ready( function() {
     $.get("/ajax_test/", function(data) {
        alert(data);
     });
 });
 </script>

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


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.

Tags:

Ajax

Django