title: error: "CSRF verification failed. Request aborted." when using jquery ajax with Django
body: <p>in template:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
          $.ajax({
               type:"POST",
               url:"{% url DrHub.views.ajxTest %}",
               data: {
                      'start': $('#id_startTime').val(),
                      'end': $('#id_endTime').val(),
                      'csrfmiddlewaretoken': '{{ csrf_token }}'
               },
               success: function(data){
                   alert(data);
               }
          });
  &lt;/script&gt;
  .
  .
  .
  &lt;form method='POST' action="."&gt;
      {% csrf_token %}
      &lt;input type="text id="id_startTime" /&gt;
      &lt;input type="text id="id_endTime" /&gt;
      &lt;input type="submit" value="send" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>in views:</p>
  
  <pre><code>def ajxTest(request):
     if request.is_ajax():
        if request.method == 'POST':
           return HttpResponse(json.dumps({'message' : 'awesome'},ensure_ascii=False), mimetype='application/javascript')
  </code></pre>
  
  <p>in settings.py:</p>
  
  <pre><code>MIDDLEWARE_CLASSES = (
      'django.middleware.common.CommonMiddleware',
      'django.contrib.sessions.middleware.SessionMiddleware',
      'django.middleware.csrf.CsrfViewMiddleware',
      'django.contrib.auth.middleware.AuthenticationMiddleware',
      'django.contrib.messages.middleware.MessageMiddleware',
      'django.contrib.messages.middleware.MessageMiddleware',
      'django.middleware.locale.LocaleMiddleware',
  )
  </code></pre>
  
  <p>when submitting form I have this error:<code>CSRF verification failed. Request aborted.</code></p>
  
  <p>I searched alot but none of suggested solutions worked for me!</p>
  
  <p>like : <a href="https://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request">Django CSRF check failing with an Ajax POST request</a></p>
  
  <p>and : <a href="https://stackoverflow.com/questions/5304517/ajax-post-in-django-framework">Ajax Post in Django framework?</a></p>
  
  <p>I refreenced to a js file with this content:</p>
  
  <pre><code>$.ajaxSetup({ 
       beforeSend: function(xhr, settings) {
           function getCookie(name) {
               var cookieValue = null;
               if (document.cookie &amp;&amp; document.cookie != '') {
                   var cookies = document.cookie.split(';');
                   for (var i = 0; i &lt; cookies.length; i++) {
                       var cookie = jQuery.trim(cookies[i]);
                       // Does this cookie string begin with the name we want?
                   if (cookie.substring(0, name.length + 1) == (name + '=')) {
                       cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                       break;
                   }
               }
           }
           return cookieValue;
           }
           if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
               // Only send the token to relative URLs i.e. locally.
               xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
           }
       } 
  });
  </code></pre>
  
  <p>but this didn't work,too!</p>
  
  <p>And I saw a solution that say use ajaxSetup instead of ajaxSend to post data,how can I do this?</p>