0

Hi I am trying to post data with AnguarJs but having some issues.My js code is below :

    var app = angular.module('myApp', []);
    app.constant('appUrl', 'http://localhost/zara/rest/');
 /*---- categories fetch code  -------*/  
    app.controller('myController',['$scope','$http','appUrl',  function($scope, $http , appUrl) {

      $scope.newName = "";
    $scope.postForm = function() {  
        var data = $.param({
                name: $scope.newName
            });

        $http.post("http://localhost/zara/rest/api_customerlogin.php", data).success(function(data, status) {
            $scope.hello = data;
            console.log(data);
        });
     };
  }]);

I am getting this error TypeError: Cannot read property 'param' of undefined And when i change code to like this one then i got this error.

<script>
    angular.module('myApp', [])
    .controller('myController', ['$scope', '$http', function($scope, $http) {
        this.loginForm = function() {

            var user_data='user_email=' +this.inputData.email+'&user_password='+this.inputData.password;

            $http({
                method: 'POST',
                url: 'login.php',
                data: user_data,
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            })
            .success(function(data) {
        console.log(data);

            })
        }

    }]);
    </script>

Cannot post to login.html page.This is my first project on AnguarJs so not very familer with this.Thanks.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Mahmood Rehman
  • 4,303
  • 7
  • 39
  • 76

2 Answers2

0

In your first code you try to post to

http://localhost/zara/rest/api_customerlogin.php

Did you forget to change login.php in your second example to this url?

Khauri
  • 3,753
  • 1
  • 11
  • 19
0

Please note that post request may not working on local environment until you add allow-orgin-access plugin for your browser. But it works on actual working websites.

var app = angular.module('myApp', []);

app.constant('appUrl', 'http://localhost/zara/rest/');

app.controller('myController',function($scope, service, appUrl){

   $scope.newName = "";
   $scope.loading = false;

   $scope.postForm = function() {

     var data = $.param({
            name: $scope.newName
         });

     $scope.loading = true;
     service.makeThisRequest(appUrl + 'api_customerlogin.php', data).then(function(data){
         $scope.hello = data;
         console.log(data);
      },function(error){
        console.log(error);
      }).finally(function(){
        $scope.loading = false;
      });

   });

}]);

app.factory('service', function ($http) {
    return {
      makeThisRequest : function(url,params){
        return $http.post(url,params);
      }
    };
});
A. Khaled
  • 1,468
  • 16
  • 28