我想用AngularJS,Node和MongoDB实现登录方法.我已经在我发送请求时构建了一个Restful API.
当我尝试执行GET请求时,控制台上会出现此错误 TypeError: UserService.logIn(...).success is not a function
成功不存在的$http
方式?
我也找到了这个,但我无法理解如何调整它以适应我的代码.
HTTP GET"类"操作:Resource.action([parameters],[success],[error])
非GET"类"操作:Resource.action([parameters],postData,[success],[error])
非GET实例操作:实例.$ action([参数],[成功],[错误])
Service.js
var appServices = angular.module('starter.services', ['ngResource']); appServices.factory('UserService', function ($resource) { return { logIn: function (email, password) { return $resource("http://localhost:3000/users/login", { email: email, password: password }); } } });
Controller.js
var apps = angular.module('starter.controller', []); apps.controller('loginCtrl', function ($scope, $ionicPopup, $state, UserService) { $scope.doLogin = function doLogin(email, password) { if (email != null && password != null) { UserService.logIn(email, password).success(function (data) { $state.go('tabs.home'); }).error(function (status, data) { var ionicPop = $ionicPopup.alert({ title: 'Login Failed', template: 'Invalid email or password.\nPlease try again!' }); console.log(status); console.log(data); }); } }; });
Shaun Scovil.. 5
您的UserService.logIn()
方法没有按预期运行的原因是,您正在返回一个新实例,Resource
但实际上从未在其上调用方法.
// This returns an instance of Resource, which is configured incorrectly. // The second argument suggests that the URL has :email and :password // parameters, which it does not. As a result, the email and password // would be appended to the URL as query params (very insecure!). return $resource("http://localhost:3000/users/login", { email: email, password: password });
随着$资源,您可以定义其他方法或修改现有的get
,save
,query
和delete
方法.但是,这仅适用于支持CRUD操作的 API端点.
对于登录调用,您不需要创建资源,因为您不会执行CRUD操作.请$http
改用.
var appServices = angular.module('starter.services', ['ngResource']); appServices.factory('authService', function ($http) { return { logIn: function (email, password) { return $http({ url: 'http://localhost:3000/users/login', method: 'POST', data: { email: email, password: password }, headers: { 'Content-Type': 'application/json' }, withCredentials: true }); } } });
上面的示例假定您要在请求正文中传递用户凭据.假设您使用SSL,这是可以的,但首选方法是使用Authorization标头.例如,Basic Auth会更安全一些.
UPDATE
事实证明,方法$resource
不返回Promise
.它们使用$promise
属性返回资源的实例.所以,你可以这样做:
// Using the .save() method, which performs a POST $resource('http://localhost:3000/users/login').save({email: 'foo@bar.com', password: 'baz'}) .$promise.then(handleSuccess, handleError);
不过,我建议使用$http
登录端点.但是,如果你需要使用$resource
,请看看这个plunker.
您的UserService.logIn()
方法没有按预期运行的原因是,您正在返回一个新实例,Resource
但实际上从未在其上调用方法.
// This returns an instance of Resource, which is configured incorrectly. // The second argument suggests that the URL has :email and :password // parameters, which it does not. As a result, the email and password // would be appended to the URL as query params (very insecure!). return $resource("http://localhost:3000/users/login", { email: email, password: password });
随着$资源,您可以定义其他方法或修改现有的get
,save
,query
和delete
方法.但是,这仅适用于支持CRUD操作的 API端点.
对于登录调用,您不需要创建资源,因为您不会执行CRUD操作.请$http
改用.
var appServices = angular.module('starter.services', ['ngResource']); appServices.factory('authService', function ($http) { return { logIn: function (email, password) { return $http({ url: 'http://localhost:3000/users/login', method: 'POST', data: { email: email, password: password }, headers: { 'Content-Type': 'application/json' }, withCredentials: true }); } } });
上面的示例假定您要在请求正文中传递用户凭据.假设您使用SSL,这是可以的,但首选方法是使用Authorization标头.例如,Basic Auth会更安全一些.
UPDATE
事实证明,方法$resource
不返回Promise
.它们使用$promise
属性返回资源的实例.所以,你可以这样做:
// Using the .save() method, which performs a POST $resource('http://localhost:3000/users/login').save({email: 'foo@bar.com', password: 'baz'}) .$promise.then(handleSuccess, handleError);
不过,我建议使用$http
登录端点.但是,如果你需要使用$resource
,请看看这个plunker.