title: Ajax call in Django without a form
body: <p>New to Ajax so I'm trying to get my head wrapped around things. I'm trying to make an Ajax call by wiring my Javascript event handler to the onclick for a span element. There's no form element in my template. Is it possible to make an Ajax call without a form element in Django? From what I understand it is, but I can't seem to get it to work.  When I take a look at the request in my view, there is no HTTP_X_REQUESTED_WITH header so the request.is_ajax() call always returns false.</p>
  
  <p>Here's the relevant portion of my template:</p>
  
  <pre><code>&lt;td&gt;&lt;span class="header" onclick="sort_user_table('username')"&gt;&lt;a href=""&gt;Username&lt;/a&gt;&lt;/span&gt;&lt;/td&gt;
  </code></pre>
  
  <p>Here's my Javascript function:</p>
  
  <pre><code>function sort_user_table(col_name) {
    $.ajax({
      url: "/listusers/",
      data: {sort_order: col_name},
      headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'},
      type: "GET",
      dataType : "json",
      success: function(json) {
        alert('json = ' + json);
      },
    });
  }
  </code></pre>
  
  <p>And here's my view:</p>
  
  <pre><code>def list_users_page(request):
    if request.is_ajax():
      sort_order = request.GET['sort_order']
      users = User.objects.all().order_by(sort_order)
      data = serializers.serialize('json', users)
      json_users = json.dumps(data)
      return HttpResponse(json_users, mimetype='application/json')
    else:
      users = User.objects.all().order_by('last_name')
      variables = RequestContext(request, {
        'users': users,
      })
      return render_to_response('users_page.html', variables)
  </code></pre>