2

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);

          });
    };

});
georgeawg
  • 48,608
  • 13
  • 72
  • 95
A.Sasori
  • 385
  • 3
  • 20
  • This issue with primitives can be easily avoided by following the "best practice" of always have a '.' in your ng-models – georgeawg May 30 '18 at 20:52

0 Answers0