Undefined error while reading 'post' property in Angularjs

Asked

Viewed 476 times

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>
<form ng-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 on the console:

Typeerror: Cannot read Property 'post' of Undefined at n.$Scope.Submit

Can someone help me?

  • Your jsfiddle code doesn’t have how you are sending the information through Angular to PHP. This is crucial to better understand the problem.

  • vc started your ng-app="myapp" in html ?

  • Yes @Ivanferrer, started.

3 answers

3


Add

$http <--- before Function, as it was not declared, the object will be Undefined, so it does not find the function . post

 .controller('myForm', ['$scope', '$http', function($scope, $http) {
    //Código aqui
  }]);
  • Thank you @Renandegrandi! Now, I can use both: then(Function(Response) { }, Function(Response) { }); as such: . Success(Function(data, status, headers, config){ console.log('Success '+data.status); }) . error( Function(data, status){ console.log('Error '+data.status); }); ?

  • yes, more actually you will use . Success(Function(return){}). error(Function(return){}), to use . then you should use $http({method:'post' ..... ). then(Function(retornoSuccess) ....

  • Since the php return has to be in json, correct @Renandegrandi?

  • yes exactly that.

  • Hi @Renandegrandi, my php json looks like this: echo json_encode( array('status' => 1, 'msg' => 'Successful registration!'); ?

  • I don’t know much php, please take the Json that is in the variable (return) within the success function and send, so I can take your doubt.

  • Next @Renandegrandi, I put these two console commands: console.log(Response.status); console.log(Response.msg); E the respective messages that appear are: 200 Undefined This 200 I believe is a positive status response, right? Already the message, it seems that he is not recognizing, that is as msg. Check out my git: https://github.com/GugaSevero/CRUD_AngularJS

  • exactly, the status == 200 (Sucess), you need to take the Response.data <-- here is your array (Response.data.msg)

  • sucess te return a JSON (date, status, headers, config, statusText), within the date, is your return (json), for more .. https://docs.angularjs.org/api/ng/service/$http

  • Thank you very much.

Show 5 more comments

0

I actually believe (I couldn’t see your code in jsfiddle) that you’re doing it right on the first line but then try to get the values in the POST. By default Angularjs plays all the information on a json in the Row body of the HTTP header and to capture it with PHP you do it the right way in the first line:

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

Try:

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

  • pq put ("php://input")?

  • Hi @Ricardo, I rephrased my question here in the post. Please see if you can help me.

  • @Gustavosevero I just edited this reply so that I had the proper formatting of the site I am not the author of it. I hope you get a resolution to your problem I know nothing of angular.

  • @Ricardo, I knew little... But take a look at this video lessons of Rodrigo Branas, you will learn very expensive https://www.youtube.com/playlist?list=PLQCmSnNFVYnTD5p2fR4EXmtlR6jQJMbPb

0

try like this:

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

  $usrname = $request-usrname;
  $upswd = $request->upswd;
  $email= $request->email;
  • I can get all the data doing so as I put it in my php. But why do it? file_get_contents("php://input"); Pq put (php://input)?

  • put php:/input do not know, I think it is to put in memory and be able to convert to json since the json_decode function receives a String, if I do not think q right you take the file, convert the bytes in String and then put inside the json_decode function.

  • Hi @Eridanimelo, I rephrased my question here in the post. Please see if you can help me.

  • hello, failed to put the $http attribute in order to be injected correctly ...controller('myForm', ['$scope','$http', function($scope, $http){...

  • reaprendi Angular via video lessons by this guy https://www.youtube.com/playlist?list=PLQCmSnNFVYnTD5p2fR4EXmtlR6jQJMbPb

Browser other questions tagged

You are not signed in. Login or sign up in order to post.