Undefined error while reading 'post' property in AngularJS

1

I have the following code:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script></head><body><formng-submit="submit()" ng-controller="myForm">
Nome<br>
<input type="text" ng-model="nome"><br>
Email<br>
<input type="text" ng-model="email"><br>
Senha<br>
<input type="password" ng-model="senha"><br>
<input type="submit" id="submit" value="Submit" />
</form>

<script type="text/javascript">
angular.module('myApp', [])
    .controller('myForm', ['$scope', function($scope, $http){
        $scope.list = [];
        $scope.submit = function(){
            $http.post('salva.php', {'nome': $scope.nome, 'email': $scope.email, 'senha': $scope.senha})
            then(function(response) {
                console.log('OK '+response);

              }, function(response) {
                console.log('Error '+response);
              });
        }
    }]);
</script>

And the following warning appears in the console:

  

TypeError: Can not read property 'post' of undefined at n. $ scope.submit

Can anyone help me?

    
asked by anonymous 07.08.2015 / 21:11

3 answers

3

Add

$ http before the function, as it was not declared, the object will be undefined, so do not find the .post function

 .controller('myForm', ['$scope', '$http', function($scope, $http) {
    //Código aqui
  }]);
    
17.08.2015 / 21:43
0

try this:

$postdata = file_get_contents("php://input");
  $request = json_decode($postdata);

  $usrname = $request-usrname;
  $upswd = $request->upswd;
  $email= $request->email;
    
07.08.2015 / 22:06
0

I actually believe (I could not see your code in jsfiddle) that you are doing correctly on the first line but then try to get the values in POST. By default angularJS throws all the information in a json in the row header of the HTTP header and to capture it with PHP you do it right on the first line:

$data = json_decode(file_get_contents("php://input"));

Try:

line01: $_POST = json_decode(file_get_contents("php://input"));

    
07.08.2015 / 21:44