angluarjs+django出现http 403(Forbidden)错误


  • angularjs向django提交post请求,出现http 403错误
Forbidden (CSRF cookie not set.): /user/add/
[15/Feb/2016 02:05:24] "POST /user/add/ HTTP/1.1" 403 2629
  • 发送post请求代码
myModule.factory('user', function($resource) {
    return $resource('user', null,{
            add: {method:'POST', "/user/add/"}
    });
});

function ($scope, user) {
    user.add($scope.project, function (success) {
            console.log('success');
        },
        function (error) {
            console.log('error');
        });
}
  • 解决方法
var app = angular.module('yourApp');
app.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.xsrfCookieName = 'csrftoken';
    $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
}]);
  • 官方参考文档
http://docs.angularjs.cn/api/ng/provider/$httpProvider

上篇: apscheduler提示maximum错误 下篇: close_fds=True与stdin/stdout/stderr不能共存