- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想用AngularJS、Node和MongoDB实现登录方法。我在发送请求时构建了一个 Restful API。
当我尝试执行 GET 请求时,此错误出现在控制台 TypeError: UserService.logIn(...).success is not a function
成功不像 $http
方式那样存在?
我也找到了这个,但我不明白如何调整它以适合我的代码。
HTTP GET“类” Action :Resource.action([参数], [成功],[错误])
非 GET“类”操作:Resource.action([parameters], postData,[成功], [错误])
非 GET 实例操作:instance.$action([parameters], [success],[错误])
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);
});
}
};
});
最佳答案
您的 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
});
有了$resource,你可以定义额外的方法或者修改现有的get
、save
、query
和delete
方法。但是,这仅对支持 CRUD operations 的 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 header 。例如,Basic Auth会更安全一些。
更新
事实证明,$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
,请查看 this plunker .
关于javascript - $resource service .success 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34256322/
我正在寻找外行人对计算机硬件和组织的介绍。以下是我想讨论的一些主题。 Brief intro to electronics. Gates and state machines, intro to re
有没有人在 Visual Basic 2010 中看到过这个错误,如果有的话......关于如何解决它的任何想法? 错误是 module 'Resources' and module 'Resourc
这个问题在这里已经有了答案: Why ?attr/colorAccent dose not work below lollipop version? (2 个答案) 关闭 5 年前。 我收到以下错误
我正在尝试通过 ring 学习 clojure 网络开发和 compojure我有点不清楚 compojure.route/resources 和 ring.middleware.resource/w
是否必须放置内部 try-with-resources 或其中一个 try-with-resources 中的所有内容都会自动关闭? try (BasicDataSource ds = Bas
我有一本包含多个食谱的 Chef 食谱,用于安装服务。 Chef-client 的工作方式是每 15 分钟(或其他固定时间间隔)重新收敛一次。现在我的食谱的第一步是停止服务,所以服务将每 15 分钟停
我有资源组“MyResources”,其中包含 4 个资源: AppService - 我的服务器, AppServicePlan - MyServerWestEuFarm, ApplicationI
我有资源组“MyResources”,其中包含 4 个资源: AppService - 我的服务器, AppServicePlan - MyServerWestEuFarm, ApplicationI
我有一个返回 ResponseEntity 的休息终点. 需要检查如何在 Swagger 规范中创建这种类型的响应。这里包中的资源是 org.springframework.core.io.Resou
In my azure portal I have 6 separate applications, I have to list all of the employed resources u
In my azure portal I have 6 separate applications, I have to list all of the employed resources u
我有一个问题,设计师不会显示表单。它失败,错误 Designer 给出警告,如下所示: 我该如何解决这个问题? 最佳答案 您似乎缺少在应用程序中加载此表单所需的项目 资源 . 您可以访问 资源右键单击
我是 angularJS 世界的新手,我可能误解了一些东西。 我的应用程序使用 Controller 、指令和服务,所有这些都运行完美,直到我使用带有 $resource 的服务,然后出现“冲突”或其
我在 Unity3D 工作。 我使用 Resources.LoadAll(path);加载文件夹和子文件夹中的所有项目。执行此操作后,我想获取对象的子文件夹名称或完整路径。这可能吗? 并且不建议使用
我需要监控每个客户端环境(一个订阅、多个资源组)的 Azure 支出。在我的研究中,我发现了 2 个可以使用的 API: 资源费率卡( https://msdn.microsoft.com/fr-fr
在 RDF 1.1 XML 语法文档中 rdf:resource 在定义 Empty Property Elements 时用作缩写形式: When a predicate arc in an RDF
这是一种常见的情况,我们需要在用户更新/创建一些数据后向用户显示错误/成功消息,我们如何在 AngularJS 中实现它? 我想添加回调但找不到解决方案。使用 $http.post().success
我正在使用 android studio 作为 IDE 开发一个 android 应用程序。 我的问题是: 如何在构建APK过程中排除某个目录下的某些文件? 在我的例子中,我想从构建中排除一些图像,因
在编译我的 Visual Studio C# 项目时,出现以下错误: 在“Resources”参数中多次指定项目“obj\Debug\SampleProject.Forms.MDIMain.resou
使用 CoffeeScript、Angular 和 $resource,我创建了以下工厂: angular.module('myapp', ['ngResource']).factory 'MyObj
我是一名优秀的程序员,十分优秀!