使用Angular Resource查询具有字符串参数的REST API
Querying a REST API with a string parameter using Angular Resource
我正在尝试查询本地托管的REST API,这应该如下所示:
http://localhost:51608/api/UserRole?email=john@example.com
该请求有效,并返回一个具有AccessLevel属性的用户对象。现在我有了一个angular应用程序,并希望使用angular Resource查询该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
当前工作,它确实用您提供给$resource
对象的电子邮件参数替换了:email
。您可以在类似%3A
的地方对:
进行编码
function userRoleResource($resource, appSettings) {
return $resource(appSettings.serverPath + "/api/UserRole%3A:email", { email: '@email'});
}
为什么不将字符串格式化为:
return $resource(appSettings.serverPath + "/api/UserRole?email:" + email});
相关文章:
- 如何使用angularjs服务将表单参数传递给Rest
- 使用Angular Resource查询具有字符串参数的REST API
- 在javascript中使用rest参数和扩展运算符
- 过滤器 AngularJS REST JSON 数据:错误:badcfg 响应与配置的参数不匹配
- 通过角度$HTTP将 URL 参数传递给 Rest 方法
- 如何在角度中获取 REST 样式参数的值
- ES6 rest 参数不适用于 babel
- 如何使用RestEasy REST.Request访问表单参数
- 在Javascript函数中使用新的ES2015 rest参数而不是使用数组参数有什么优点
- 无法使用参数从html/javascript调用rest web服务
- ES6:如果rest参数是一个数组会发生什么?
- 从除了页面参数之外没有分页的REST API获取数据
- 将数据存储在一个变量中,以便在运行frisby测试之前在REST API路由中作为参数使用
- 可以将rest参数用作解构赋值吗?
- 如何创建一个将参数传递给rest web服务的控制器?
- 在ES6中是否可以有超过rest参数作为函数参数?
- 主干模型-根据REST操作更改URL查询参数
- 如何使用URI参数调用WCF REST-Service
- AngularJS REST POST多个参数
- 函数rest参数抛出错误