Skip to content

Instantly share code, notes, and snippets.

@bgloh
Created January 11, 2019 03:32
Show Gist options
  • Save bgloh/b287b0a9ffb02693c459c7df3b41daf8 to your computer and use it in GitHub Desktop.
Save bgloh/b287b0a9ffb02693c459c7df3b41daf8 to your computer and use it in GitHub Desktop.
Django-AngularJS : csrf token, http Content-Type : 'application/x-www-form-urlencoded',
<html ng-app='myApp'>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.js">
</script>
<script
src="https://code.jquery.com/jquery-1.12.4.js"
integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU="
crossorigin="anonymous"></script>
</head>
<div ng-controller="myController1">
<p>This is index.html</p> <br>
<p>value1 : [{ var1 }] </p>
</div>
</html>
<script>
var app = angular.module('myApp', [], function($interpolateProvider) {
$interpolateProvider.startSymbol('[{');
$interpolateProvider.endSymbol('}]');
})
app.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
}]);
app.controller('myController1', function($scope,$http){
$scope.var1 = 1999.2345;
// http POST
$http({
method: 'POST',
url: 'getResponse',
data: $.param({'info' : $scope.var1, 'd': 123.456 })
}).then(function(response) {
console.log(response);
}, function(error) {
console.log(error);
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment