0

I try use $http.delete with Django and get HTTP 403. My JS file below:

   var myApp = angular.module('myApp',['ui.bootstrap']);

    myApp.run(function($http) {
        $http.defaults.headers.post['X-CSRFToken'] = $.cookie('csrftoken');
    });


    myApp.controller('PostsListController', ['$scope','$http',function($scope,$http) {
      $http.get('/api/v1/posts/').success(function (data) {

      $scope.posts = data;

      });
    $scope.deletePost = function(post) {
                $http.delete('/api/v1/posts/'+ post.id + '/');
    };

    }]);

detail from console:

{detail: "CSRF Failed: CSRF token missing or incorrect."}
detail
:
"CSRF Failed: CSRF token missing or incorrect."
5
  • Check your CSRF Token. It might not be set in the cookie. Commented May 23, 2016 at 15:52
  • Are you sure the cookie exists and is being set? Commented May 23, 2016 at 15:54
  • I know that the problem in token, but I do not know what value to set Commented May 23, 2016 at 15:54
  • Not sure this is an angular question then. IE: Your title is misleading. Commented May 23, 2016 at 16:00
  • The Django documentation provides more information on retrieving the CSRF token and sending it in ajax requests. The CSRF token is saved as a cookie called csrftoken that you can retrieve from a HTTP response, which varies depending on the language that is being used.. More: docs.djangoproject.com/en/dev/ref/csrf/#ajax Commented May 23, 2016 at 16:03

1 Answer 1

1

i solved my problem by rewriting method .run()

 myApp.run(function($http) {
  $http.defaults.xsrfCookieName = 'csrftoken';
$http.defaults.xsrfHeaderName = 'X-CSRFToken';
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.