Dirk Dunn Dirk Dunn - 1 year ago 183
AngularJS Question

$http.post undefined is not a function

I am trying to implement a simple $http post with angular within this controller.

$scope.isAdmin = admin.isAdmin;
$scope.logout = admin.logout;

$scope.login = function(){
if(!$scope.username || !$scope.password ){ return; }
$http.post('/login',{ username: $scope.username, password: $scope.password });
$scope.username = '';
$scope.password = '';

This part

$http.post('/login',{ username: $scope.username, password: $scope.password });

is throwing this error

TypeError: undefined is not a function
at l.$scope.login (http://localhost:3000/javascripts/angularApp.js:165:9)
at hb.functionCall (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:198:426)
at Cc.(anonymous function).compile.d.on.f (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:214:485)
at l.$get.l.$eval (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:126:193)
at l.$get.l.$apply (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:126:419)
at HTMLFormElement.<anonymous> (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:215:36)
at HTMLFormElement.c (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:32:363)

here is the HTML for the ui-view.

<script type="text/ng-template" id="/login.html">
<button id="logout" class="btn btn-default" ng-click="logout()" ng-show="isAdmin()">Log Out</button>
<div ng-controller="LoginCtrl">
<form id="login" ng-submit="login()">
<h2>The Login Page.</h2>
<input type="text" ng-model="username" placeholder="username"/>
<input type="password" ng-model="password" placeholder="password"/>
<button type="submit" class="btn btn-primary">Submit</button>

I have checked the syntax, everything looks correct, $http is being recognized as an object when I console.log it within the controllers scope.Yet for some reason I cannot make this post request because an error is being thrown. I am using expressjs to serve the content, and my current route for '/login' is just a simple response.send('response sent!'). As you can see in the HTML I am using angular ui-router, in which I also have a .state set for it, if this needs to be provided please let me know.

I have been frustrated about this because this is clearly known function, and I cannot find any resources online to help me with the exact problem. I have tried setting the content type in the headers, and other online solutions but nothing has been helping. It seems like it may be something stupid, but I can't find it.

I would appreciate any input or help, thank you.

Answer Source

Dependency injection order is wrong

app.controller('LoginCtrl', ['$scope', 'admin', '$http',function($scope, $http, admin) { ...

should be

app.controller('LoginCtrl', ['$scope', '$http', 'admin', function($scope, $http, admin) { ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download