1

I'm using django restframe work,When I use browser to sent PUT request, it works well.but When I using ajax sent request just like before, it tell me csrf token is missing or incorrected.

My jQuery code looks like this:

$.ajax({
    url: "/api/users/1/",
    type: "PUT",
    datatype: "json",
    data:  {csrfmiddlewaretoken: csrf_token, "user_gender": 2, "user_details": 23 },
....

In chrome console

csrfmiddlewaretoken:wUlPIaEmZd2FMA2ob9VRSVKWpOf6EQHn
user_gender:0
user_details:111asdfasdfsdf

I'd console.log(csrf_token), and it is as same as which I using in brower.

I followed Bryan here Django CSRF check failing with an Ajax POST request and it works in other page using POST request.

Windsooon
  • 6,864
  • 4
  • 31
  • 50

1 Answers1

1

Django documentation says, that you may put csrf-token in X-CSRFToken header in your AJAX request: https://docs.djangoproject.com/en/dev/ref/csrf/#ajax

  • I followed Bryan here https://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request and it works in other page using POST request. – Windsooon Mar 06 '16 at 13:55