Skip to content

Instantly share code, notes, and snippets.

@macndesign
Created July 15, 2014 15:22
Show Gist options
  • Save macndesign/b1f61930c4ec1e19fae2 to your computer and use it in GitHub Desktop.
Save macndesign/b1f61930c4ec1e19fae2 to your computer and use it in GitHub Desktop.
Test DRF + AngularJS - Error 500 in post data
<!doctype html>
<html lang="en" ng-app="App">
<head>
<meta charset="UTF-8">
<title>Example DRF and AngularJS</title>
</head>
<body ng-controller="DemoController as demo">
<div>
<ul>
<li ng-repeat="user in users">{$ user.email $}</li>
</ul>
<button ng-click="createUser()">Add</button>
<button ng-click="deleteUser()">Delete</button>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.15/angular.min.js"></script>
<script src="https://code.angularjs.org/1.2.9/angular-cookies.min.js"></script>
<script>
(function(){
var app = angular.module('App', ['ngCookies']);
app.config(function($httpProvider) {
$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
});
app.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('{$');
$interpolateProvider.endSymbol('$}');
});
app.controller('DemoController', ['$scope', '$http', '$window', '$cookies', function($scope, $http, $window, $cookies) {
$scope.users = [];
$http.get('/api/users/').success(function(data){
console.log(data.results);
$scope.users = data.results;
});
$scope.createUser = function () {
var data = {
"username": "test",
"email": "[email protected]",
"groups": null
};
$http({
url: '/api/users/',
dataType: 'json',
method: 'POST',
data: data,
headers: {
"Content-Type": "application/json"
}
}).success(function(response){
$window.alert('Success!', response);
$window.location.reload();
}).error(function(error){
$window.alert('Error!', error);
});
};
$scope.deleteUser = function () {
$http.delete('/api/users/3/').success(function(){
$window.alert('Deleted!');
$window.location.reload();
});
};
}]);
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment