Search code examples
jquerydjangocsrfdjango-csrf

Django + jQuery: Why CSRF verification fails on multiple simultaneous requests


I've encountered the following situation (using Django1.4 and jQuery 1.7.1) which I would like to understand: I submit a form via jQuery 'ajax' function and before this request finishes I click another element which submits the same form but using the jQuery 'submit' function. The response I get - 403, CSRF verifcation failed. Now, of course, to prevent this error it suffices to disable multiple simultaneous submits(they work fine on their own), but this doesn't help to understand where that specific error comes from.

Can anyone explain this? The csrf token is generated once per session so it cannot be some kind of csrf mismatch for the latter request. Does this have something to do with the way jQuery handels requests?


Solution

  • Problem solved - I missed the fact that the js script responsible for the ajax call set the 'disabled' attribute on the form inputs (including the csrf token) in the beforeSend function - such disabled attrs are not sent in POST.