我正在尝试查询我在本地托管的 REST API,这应该按如下方式工作:
http://localhost:51608/api/UserRole?email=john@example.com
该请求有效,并返回一个具有 AccessLevel 属性的用户对象。现在我有一个 Angular 应用程序,并且想要使用 Angular 资源查询该 URL。我有如下内容,但 URL 未正确构建。
(function () {
"use strict";
angular
.module("common.services")
.factory("userRoleResource",
["$resource",
"appSettings",
userRoleResource])
function userRoleResource($resource, appSettings) {
return $resource(appSettings.serverPath + "/api/UserRole:email", { email: '@email'});
}
})();
我对上面的查询如下:
userRoleResource.query({ email: vm.userData.email },
function (data) {
vm.userData.accessLevel = data.AccessLevel;
});
但这会生成如下请求:
http://localhost:51608/api/UserRolejohn@example.com而不是http://localhost:51608/api/UserRole?email:john@example.com
这里出了什么问题?
最佳答案
根据 $resource
当前工作,它确实将 :email
替换为您提供给 $resource
对象的电子邮件参数。您可以在 %3A
:
function userRoleResource($resource, appSettings) {
return $resource(appSettings.serverPath + "/api/UserRole%3A:email", { email: '@email'});
}
关于javascript - 使用 Angular Resource 通过字符串参数查询 REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36721925/