Skip to content

Instantly share code, notes, and snippets.

@kayzhu
Last active September 27, 2016 10:11
Show Gist options
  • Save kayzhu/4714734 to your computer and use it in GitHub Desktop.
Save kayzhu/4714734 to your computer and use it in GitHub Desktop.
Send POST request in Angular.js to a CSRF-protected Django view
<script type="text/javascript" src="/angular/angular.js"></script>
<script type="text/javascript" src="/angular/angular-cookies.min.js"></script>
// app.js
// inject ngCookies to your app named 'myApp'.
angular.module('myApp', ['ngCookies']);
// controller.js
function MyCtrl($scope, $http, $cookies) {
var my_url = $scope.url;
var my_data = $scope.my_data;
$http({
url: my_url,
data: my_data,
method: 'POST',
headers: {
'X-CSRFToken': $cookies['csrftoken']
}
}).
success(function(data) {
console.log(data);
}).
error(function(data) {
console.log(data);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment