angular 上未定义的 http 函数
http function undefined on angular
我是 angular 和 MEAN 堆栈的新手,在此之前,我在这里问了一个问题:
Angular Routing ngRoute fails to pull my other HTML files
基本上,我没有正确设置我的应用程序路由,解决方案是我必须将我的 link 修改为 #!来自 #,那里有人说这是由 Angular 1.6 中的重大更改引起的,在此之前我有一个有效的实现,但它不是一个合适的单页应用程序,因为它没有只拉出合适的 HTML 的应用程序。好的,现在我可以查看和导航该应用程序了。
然后我 运行 遇到了另一个问题,当我尝试与应该将我注册到该应用程序中的应用程序通信时,当我单击 HTML 页面上的按钮时
<form class="form-auth" ng-submit="register()">
<h2>Register</h2>
<p class="text-warning">{{error_message}}</p>
<input type="username" ng-model="user.username" placeholder="Username" class="form-control"><br>
<input type="password" ng-model="user.password" placeholder="Password" class="form-control"><br>
<input type="submit" value="Register" class="btn btn-primary" />
我在控制台日志中收到一条错误消息,说
Error: $http.post(...).success is not a function
at b.$scope.register (iotApp.js:73)
at fn (eval at compile (angular.js:15152), <anonymous>:4:144)
at e (angular.js:26673)
at b.$eval (angular.js:17958)
at b.$apply (angular.js:18058)
at HTMLFormElement.<anonymous> (angular.js:26678)
at bg (angular.js:3613)
at HTMLFormElement.d (angular.js:3601)(anonymous function) @ angular.js:14324(anonymous function) @ angular.js:10834$apply @ angular.js:18063(anonymous function) @ angular.js:26678bg @ angular.js:3613d @ angular.js:3601
这是控制台指示错误的代码部分
app.controller('authController', function($scope, $http, $rootScope, $location){
$scope.user = {username: '', password: ''};
$scope.error_message = '';
$scope.login = function(){
$http.post('/auth/login', $scope.user).success(function(data){
if(data.state == 'success'){
$rootScope.authenticated = true;
$rootScope.current_user = data.user.username;
$location.path('/');
}
else{
$scope.error_message = data.message;
}
});
};
$scope.register = function(){
$http.post('/auth/signup', $scope.user).success(function(data){
if(data.state == 'success'){
$rootScope.authenticated = true;
$rootScope.current_user = data.user.username;
$location.path('/');
}
else{
$scope.error_message = data.message;
}
});
};
});
我已经阅读了其他一些答案,但其中大多数不小心将依赖项放在了已导入控制器的方法参数中。当我使用 Advanced REST Client 时它工作正常,这是一个 chrome 扩展来手动发送注册请求,但当我使用该应用程序时却不行。任何帮助或只是一般的指示将不胜感激。谢谢!
来自 $http.post
文档:
// Simple GET request example:
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
我是 angular 和 MEAN 堆栈的新手,在此之前,我在这里问了一个问题: Angular Routing ngRoute fails to pull my other HTML files
基本上,我没有正确设置我的应用程序路由,解决方案是我必须将我的 link 修改为 #!来自 #,那里有人说这是由 Angular 1.6 中的重大更改引起的,在此之前我有一个有效的实现,但它不是一个合适的单页应用程序,因为它没有只拉出合适的 HTML 的应用程序。好的,现在我可以查看和导航该应用程序了。
然后我 运行 遇到了另一个问题,当我尝试与应该将我注册到该应用程序中的应用程序通信时,当我单击 HTML 页面上的按钮时
<form class="form-auth" ng-submit="register()">
<h2>Register</h2>
<p class="text-warning">{{error_message}}</p>
<input type="username" ng-model="user.username" placeholder="Username" class="form-control"><br>
<input type="password" ng-model="user.password" placeholder="Password" class="form-control"><br>
<input type="submit" value="Register" class="btn btn-primary" />
我在控制台日志中收到一条错误消息,说
Error: $http.post(...).success is not a function
at b.$scope.register (iotApp.js:73)
at fn (eval at compile (angular.js:15152), <anonymous>:4:144)
at e (angular.js:26673)
at b.$eval (angular.js:17958)
at b.$apply (angular.js:18058)
at HTMLFormElement.<anonymous> (angular.js:26678)
at bg (angular.js:3613)
at HTMLFormElement.d (angular.js:3601)(anonymous function) @ angular.js:14324(anonymous function) @ angular.js:10834$apply @ angular.js:18063(anonymous function) @ angular.js:26678bg @ angular.js:3613d @ angular.js:3601
这是控制台指示错误的代码部分
app.controller('authController', function($scope, $http, $rootScope, $location){
$scope.user = {username: '', password: ''};
$scope.error_message = '';
$scope.login = function(){
$http.post('/auth/login', $scope.user).success(function(data){
if(data.state == 'success'){
$rootScope.authenticated = true;
$rootScope.current_user = data.user.username;
$location.path('/');
}
else{
$scope.error_message = data.message;
}
});
};
$scope.register = function(){
$http.post('/auth/signup', $scope.user).success(function(data){
if(data.state == 'success'){
$rootScope.authenticated = true;
$rootScope.current_user = data.user.username;
$location.path('/');
}
else{
$scope.error_message = data.message;
}
});
};
});
我已经阅读了其他一些答案,但其中大多数不小心将依赖项放在了已导入控制器的方法参数中。当我使用 Advanced REST Client 时它工作正常,这是一个 chrome 扩展来手动发送注册请求,但当我使用该应用程序时却不行。任何帮助或只是一般的指示将不胜感激。谢谢!
来自 $http.post
文档:
// Simple GET request example:
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});