title: Ajax POST with csrfmiddlewaretoken and csrftoken cookie set still gets django 403 Forbidden
body: <p>I've read that both the csrfmiddlewaretoken and the csrftoken cookie have to be the correct value for a django POST request to succeed (<a href="https://stackoverflow.com/questions/5588374/django-csrftoken-cookie-vs-csrfmiddlewaretoken-html-form-value">django: csrftoken COOKIE vs. csrfmiddlewaretoken HTML Form value</a>). This is the case for me yet I still get a 403:</p>
  
  <p>In the chrome console: </p>
  
  <pre><code>document.cookie
  </code></pre>
  
  <p>returns </p>
  
  <pre><code>"csrftoken=Wt9eeJop5Vb3OmeNTvogegckm1pVM5MD"
  </code></pre>
  
  <p>but </p>
  
  <pre><code>$.get('https://learningdollars.fwd.wf/csrftoken/', function(data){
      console.log(data)
      token = $.parseHTML(data)[0].value
      console.log(token)
      $.ajax({
          type: "POST",
          url: 'https://learningdollars.fwd.wf/confirmemail/',
          data: {email: 'gdasu@alumni.stanford.edu', csrfmiddlewaretoken: token},
          contentType: "application/json"
      })
      .done(function(response) {
          console.log('done!')
      })
      .fail(function(error) {
          console.log('fail!')
      })
  })
  </code></pre>
  
  <p>returns </p>
  
  <pre><code>&gt; Object {readyState: 1, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}
  &gt; &lt;input type='hidden' name='csrfmiddlewaretoken' value='Wt9eeJop5Vb3OmeNTvogegckm1pVM5MD' /&gt;
  &gt; Wt9eeJop5Vb3OmeNTvogegckm1pVM5MD
  &gt; POST https://learningdollars.fwd.wf/confirmemail/ 403 (Forbidden)
  &gt; fail! 
  </code></pre>
  
  <p>I have </p>
  
  <pre><code>'django.middleware.csrf.CsrfViewMiddleware',
  </code></pre>
  
  <p>activated in my django middleware.</p>
  
  <p>My root urls.py contains:</p>
  
  <pre><code>url(r'^csrftoken/$', views.get_csrf_token),
  url(r'^confirmemail/$', views.confirm_email, name='confirm_email'),
  </code></pre>
  
  <p>And, my views are: </p>
  
  <pre><code>def get_csrf_token(request):
      c = {}
      c.update(csrf(request))
      print c
      return render_to_response('csrf.html', c)
  
  def confirm_email(request):
      print 'here'
      return JsonResponse({'response': 0})
  </code></pre>
  
  <p>And by the way, the contents of csrf.html are just the csrftoken (inside an input tag):</p>
  
  <pre><code>{% csrf_token %}
  </code></pre>
  
  <p>What am I doing wrong?</p>