i am trying to post data to my database from my html form through the api endpoint using angularjs? here's the angularjs code
angular.module('myApp').config(function ($routeProvider) {
$routeProvider.when('/userurl', {template :
"<form>" +
"<p>name:<input type='text' ng-model='myUser' value=''></p>" +
"<input type='submit' value='add user' ng-click='postUser()'>" +
"</form>" });
});
and here is the function i created for the ng-click to post the data
angular.module('myApp').controller('myAppCtrl', function ($scope, $http) {
$scope.postUser = function () {
$http({
url : "http://127.0.0.1:8000/users/",
method: "POST",
data : {
username : $scope.myUser
},
headers : {'Content-Type' : 'application/json', 'Accept':'application/json'
}
}).then(function onSuccess(response) {
console.log(response);
}).catch(function onError(error) {
console.log(error);
});
};
});