XSRF headers not being set in AngularJS

前端 未结 5 1181
名媛妹妹
名媛妹妹 2020-12-10 05:38

I\'m developing a DJANGO + AngularJS application, where the angular part is not being served by django.

I set the angular $httpProvider as follows:

5条回答
  •  清歌不尽
    2020-12-10 05:49

    The 1.2.0 update wasn't sufficient for me when using Safari or Firefox (Chrome was working just fine all the time). The problem with Safari and Firefox was that the Django backend didn't send the csrf-cookie in the HTTP response.

    What I had to do was add the @ensure_csrf_cookie decorator to my view function that builds up the page for Angularjs.

    @ensure_csrf_token
    def my_view(request):
        ...
    

    and in the javascript file:

    myApp.config(function($httpProvider) {
        $httpProvider.defaults.xsrfCookieName = 'csrftoken';
        $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
    }
    

    At least for now I have no idea why Chrome works without it but the other browsers don't.

提交回复
热议问题