I want to post data to django app, I use angularjs http to post. When I post data, I get a 403 forbidden. So, I changed MY angular script to this:
var app = angular.module('omCenterLoginApp',['ngCookies']);
app.config(['$httpProvider', function ($httpProvider,$cookies) {
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
}])
app.controller('omCenterLoginContr',function($scope,$http){
$scope.login = function(){
var user = $scope.username;
var password = $scope.password;
$http({
method: 'POST',
url:'http://172.100.1.8:8080/api/test/',
data:{user:password},
}).then(function successCallback(response){
console.log(response.data)
},function errorCallback(response){
console.log(response.data)
})
}
});
I added,
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken'; $httpProvider.defaults.xsrfCookieName = 'csrftoken';
but, when I tried to post it, still got response a 403 error. I checked the post http header. There is no X-CSRFToken in it.
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.8,en;q=0.6
Cache-Control:no-cache
Connection:keep-alive
Content-Length:2
Content-Type:application/json;charset=UTF-8
Host:172.100.1.8:8080
Origin:http://172.100.1.8
Pragma:no-cache
Referer:http://172.100.1.8/login.html?
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
My angularjs version:1.5.8
But when I tried to visit the url http://172.100.1.8:8080/api/test/,I can get the response .
The http request is :
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.8,en;q=0.6
Cache-Control:no-cache
Connection:keep-alive
Cookie:sessionid=jbg90n8118w6qhcmbom8txctui2s59ca; csrftoken=tBsTGiBtcgMOw8MFslu6v3UJEdSXkmYF
Host:172.100.0.48:8080
Pragma:no-cache
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
The csrftoken is exist.
Thank you for your help in advance.