我尝试在Django上使用$ http.delete并获取HTTP 403。 我的JS文件如下:
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: "CSRF Failed: CSRF token missing or incorrect."}
detail
:
"CSRF Failed: CSRF token missing or incorrect."
答案 0 :(得分:1)
我通过重写方法解决了我的问题.run()
myApp.run(function($http) {
$http.defaults.xsrfCookieName = 'csrftoken';
$http.defaults.xsrfHeaderName = 'X-CSRFToken';
});