Django 1.9 AJAX form CSRF token 403 error - “CSRF cookie not set”

后端 未结 1 1360
日久生厌
日久生厌 2020-12-31 15:05

I\'ve seen a lot about this on SO, but nothing can fix my problem.

Problem:

With CSRF middleware enabled, Django responds with 403 on AJAX f

相关标签:
1条回答
  • 2020-12-31 15:49

    Ok, the issue is quite simple then:

    Fetch API is not sending credentials by default. According to MDN:

    The credentials read-only property of the Request interface indicates whether the user agent should send cookies from the other domain in the case of cross-origin requests. This is similar to XHR’s withCredentials flag, but with three available values.

    Default is omit, and it never sends cookies. You just need to add same-origin to your fetch() function arguments:

    fetch(formUrl, {
        ...
        credentials: 'same-origin',
        ...
    })
    

    And you'll be good to go : )

    0 讨论(0)
提交回复
热议问题