简体   繁体   English

angular.js:12520 TypeError:无法读取未定义的属性'post'

[英]angular.js:12520 TypeError: Cannot read property 'post' of undefined

I am very much new to angularjs, i have look many webpages but not get any concrete solutions. 我对angularjs非常陌生,我浏览了许多网页,但没有任何具体的解决方案。

What i am trying to achieve is i am creating one factory service to authenticate the user. 我要实现的目标是创建一项工厂服务来验证用户身份。 After successful authentication it navigate to dashboard page. 认证成功后,它导航到仪表板页面。

i am getting the following error: 我收到以下错误:

angular.js:12520 TypeError: Cannot read property 'post' of undefined
    at Object.factory.authentication (login.html:74)
    at r.$scope.loginCall (login.html:65)
    at fn (eval at compile (angular.js:13365), <anonymous>:4:218)
    at e (angular.js:23613)
    at r.$eval (angular.js:16052)
    at r.$apply (angular.js:16152)
    at HTMLInputElement.<anonymous> (angular.js:23618)
    at HTMLInputElement.dispatch (jquery-1.9.1.min.js:3)
    at HTMLInputElement.v.handle (jquery-1.9.1.min.js:3)

My Html Code is : 我的HTML代码是:

    <div ng-app="loginFormApp" ng-controller="loginFormCtrl">

<form method="post" action="" id="login_form" class="row">
                            <input type="text" placeholder="Login ID" ng-model="loginId" >
                            <input type="password" placeholder="Password" ng-model="password" >
                            <input type="button" class="btn btn-theme" ng-click="loginCall()" value="Login">
                            <input type="button" class="btn btn-theme" ng-click="loginCall()" value="Register Here">
                        </form>
                        </div>

my controller is : 我的控制器是:

 var app = angular.module('loginFormApp', []);
            app.controller('loginFormCtrl', function($scope, $http, AuthService) {
                $scope.loginCall = function() {

                    AuthService.authentication();

                };
            });

My Factory Service is : 我的工厂服务是:

 app.factory('AuthService', function() {
            var factory = {};
            factory.authentication = function($http) {
                //alert("hello1");
                $http.post("http://103.19.89.152:8080/ccp-services/authenticate", {
                        'userName': $scope.loginId,
                        'password': $scope.password
                    })
                    .then(function(response) {

                        window.location.href = "http://103.19.89.152:8080/earnfit/ui/angular/dashboard.html";
                    });
            };
            return factory;
        });

In the Factory method, you are not injecting $http and hence its undefined when you access it. 在Factory方法中,您没有注入$ http,因此在访问它时未定义它。

Please refer the below code 请参考以下代码

app.factory('AuthService', function ($http) {
return {
    authentication : function (UserName, Password) {
        $http.post("http://103.19.89.152:8080/ccp-services/authenticate", { 'userName': UserName, 'password': Password})
            .then(function (response) { window.location.href = "http://103.19.89.152:8080/earnfit/ui/angular/dashboard.html";}, 
                  // Error Handling
                  function (response) {console.log(response)});
    }
  }
});

So, if you observe I removed the scope and instead I am passing the login data as parameters which will work as injecting $scope is not recommended. 因此,如果您观察到我删除了作用域,而是将登录数据作为参数传递,则建议不要像注入$ scope一样工作。

-- Farhan -法汉

Add $http as dependency on line in factory. 在工厂中添加$http作为对行的依赖。

app.factory('AuthService', function($http) {

Regards. 问候。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 angular.js:12520 TypeError:无法读取r。$ scope.insertvalue处未定义的属性&#39;option1&#39; - angular.js:12520 TypeError: Cannot read property 'option1' of undefined at r.$scope.insertvalue $ window.location.href(angular.js:12520 TypeError:无法设置未定义的属性&#39;href&#39;) - $window.location.href (angular.js:12520 TypeError: Cannot set property 'href' of undefined) angular.js:13550 TypeError:无法读取未定义的属性“编译”? - angular.js:13550 TypeError: Cannot read property 'compile' of undefined? Angular.js $ http.post TypeError:无法读取未定义的属性“data” - Angular.js $http.post TypeError: Cannot read property 'data' of undefined angular.js:14110 TypeError:无法读取undefined的属性&#39;push&#39; - angular.js:14110 TypeError: Cannot read property 'push' of undefined Angular.js:10671 TypeError:无法读取未定义的属性 - Angular.js:10671 TypeError: Cannot read property of undefined 我不断收到此错误“ angular.js:TypeError:无法读取未定义的属性&#39;pages&#39;” - I keep getting this error “angular.js:TypeError: Cannot read property 'pages' of undefined” angular.js:13550 TypeError:无法设置未定义的属性“people” - angular.js:13550 TypeError: Cannot set property 'people' of undefined 即时通讯收到错误。 angular.js:13550 TypeError:无法读取未定义的属性&#39;$ invalid&#39; - im getting error. angular.js:13550 TypeError: Cannot read property '$invalid' of undefined Angular.JS Uncaught TypeError:无法分配给只读属性 - Angular.JS Uncaught TypeError: Cannot assign to read only property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM