我是Angularjs的新手,我正在使用我在网上看到的教程,我在本节中不断得到TypeError,
angular.js:14800 TypeError:$ http.post(...)。success不是函数 at Object.authFactory.login(authService.js:15) 在Object.vm.doLogin(mainCtrl.js:25)
请在下面找到我的代码:
的index.html
<!Doctype html>
<html ng-app="MyApp">
<head>
<!-- <> -->
<title>user story </title>
<script type ="text/javascript" src='https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js'></script>
<script type ="text/javascript" src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular-route.min.js'></script>
<!-- services -->
<script type="text/javascript" src="app/services/authService.js"></script>
<!-- controllers -->
<script type="text/javascript" src="app/controllers/mainCtrl.js"></script>
<script type="text/javascript" src="app/app.routes.js"></script>
<script type="text/javascript" src="app/app.js"></script>
</head>
路由
angular.module('MyApp', ['appRoutes', 'mainCtrl', 'authService']);
authService
angular.module('authService',[])
.factory('Auth', function($http, $q, AuthToken){
var authFactory = {};
authFactory.login = function(username, password){
return $http.post('/api/login',{
username: username,
password: password
})
.success(function(data){
AuthToken.setToken(data.token);
return data;
})
});
我的控制器
angular.module('mainCtrl', [])
.controller('MainController', function($rootScope, $location, Auth){
var vm = this;
vm.doLogin = function(){
vm.processing = true;
vm.error = '';
Auth.login(vm.loginData.username, vm.loginData.password)
.success(function(data){
vm.processing = false;
Auth.getUser()
.then(function(data){
vm.user = data.data;
});
if(data.success)
$location.path('/');
else
vm.error = data.message;
});
}
谢谢!