title: How to get the token with django rest framework and ajax
body: <p>I want to build a rest api where users can authenticate with tokens. I have included <code>rest_framework.authtoken</code> in the installed apps list. Also added the required configuration in the settings.py:</p>
  <pre><code>INSTALLED_APPS = (
      ...
      'rest_framework.authtoken'
  )
  
  REST_FRAMEWORK = {
      'DEFAULT_PERMISSION_CLASSES': (
          'rest_framework.permissions.IsAuthenticated',
      ),
      'DEFAULT_AUTHENTICATION_CLASSES': (
          'rest_framework.authentication.TokenAuthentication',
          'rest_framework.authentication.SessionAuthentication',
      )
  }
  </code></pre>
  <p>Defined a method to listen to <code>post_save</code> signal and create new token for the newly created user. Then I did migration. After creating new user I can see the token for that user.</p>
  <p>Also if I do</p>
  <pre><code>http POST 0.0.0.0:80/api-token-auth/ username='user@gmail.com' password='secure123'
  </code></pre>
  <p>I get a this response back</p>
  <pre><code>HTTP/1.0 200 OK
  Allow: POST, OPTIONS
  Content-Type: application/json
  Date: Sat, 30 Jul 2016 12:05:30 GMT
  Server: WSGIServer/0.1 Python/2.7.3
  Vary: Cookie
  X-Frame-Options: SAMEORIGIN
  
  {
      &quot;token&quot;: &quot;4aecfb249265064c55300d782e4c7e66b8b77063&quot;
  }
  </code></pre>
  <p>So I suppose its working. But if I try to login with ajax:</p>
  <pre><code>$.ajax({
      url: 'http://test.com/api-token-auth/ username=' + email + ' password='+ password,
      dataType: 'json',
      cache: false,
      success: function(data) {
          console.log(data);
      }.bind(this),
      error: function(xhr, status, err) {
          console.log(err);
      }.bind(this)
  });
  </code></pre>
  <p>I get this error in the browser console:</p>
  <blockquote>
  <p>jquery-3.1.0.min.js:4 GET <a href="http://test.com/api-token-auth/%20username=user@gmail.com%20password=secure123?_=1469883569618" rel="nofollow noreferrer">http://test.com/api-token-auth/%20username=user@gmail.com%20password=secure123?_=1469883569618</a> 405 (Method Not Allowed)</p>
  <p>bundle.js:27453 Method Not Allowed</p>
  </blockquote>
  <p>How do I get the token for authenticated user so that I can use it to post as authenticated user?</p>
  <p><strong>update</strong></p>
  <p>Also I am using <a href="https://github.com/ottoyiu/django-cors-headers" rel="nofollow noreferrer">django-cors-headers</a> to deal with CORS related problem.</p>
  <p><strong>update</strong></p>
  <pre><code>%20username=user@gmail.com%20password=secure123?_=1469885103431 405 xhr jquery-3.1.0.min.js:4   278 B   29 ms   
  </code></pre>
  <p><strong>update: added response header</strong></p>
  <p><a href="https://i.stack.imgur.com/Qx7lE.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qx7lE.jpg" alt="enter image description here" /></a></p>