Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django/AngularJS:CSFR令牌错误_Python_Angularjs_Django_Cookies_Csrf - Fatal编程技术网

Python Django/AngularJS:CSFR令牌错误

Python Django/AngularJS:CSFR令牌错误,python,angularjs,django,cookies,csrf,Python,Angularjs,Django,Cookies,Csrf,我对Django和AngularJS都是新手,我已经为此奋斗了几个小时 要发布到Django服务器的AngularJS代码(我的控制器): $http({ method: 'POST', url: 'http://127.0.0.1:8000/polls/', // This is the Django server IP data: {'string': 'body'} }).then(function successCallback(response) {

我对Django和AngularJS都是新手,我已经为此奋斗了几个小时

要发布到Django服务器的AngularJS代码(我的控制器):

$http({
    method: 'POST',
    url: 'http://127.0.0.1:8000/polls/', // This is the Django server IP
    data: {'string': 'body'}
    }).then(function successCallback(response) {
     $scope.var= "success";
  }, function errorCallback(response) {
    $scope.var= response; // To display error. 
     });

    }
})
Django服务器代码(视图中):

我得到的确切错误是: 403号岗位(禁止)

错误详细信息-CSRF验证失败。请求中止。您看到此消息是因为此站点在提交表单时需要CSRF cookie。出于安全原因,需要使用此cookie,以确保您的浏览器不会被第三方劫持。胡说八道

编辑
如果您有激活cors头文件,则希望解决方案能够在不影响Django的任何安全规定的情况下工作

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def index(request):
    return "true"

您必须将令牌csrf包含在post调用的标头中

    var csrf ='{{ csrf_token }}';
    $http({
        method: 'POST',
        headers: {'X-CSRFToken' : csrf },
        url: 'http://127.0.0.1:8000/polls/', // This is the Django server IP
        data: {'string': 'body'}
        }).then(function successCallback(response) {
         $scope.var= "success";
      }, function errorCallback(response) {
        $scope.var= response; // To display error. 
         });

        }
    })
或为该呼叫使用csrf令牌

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def index(request):
    return "true"

是的,我已经激活了。但是如果没有csrf_豁免,是否有办法做到这一点,因为我希望保持它的安全。而且,即使尝试csrf_豁免,也会导致内部服务器错误
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def index(request):
    return "true"