javascript - 使用 Angular Resource 通过字符串参数查询 REST API

标签 javascript angularjs rest

我正在尝试查询我在本地托管的 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/

相关文章:

javascript - 防止呈现给定日期之前发生的事件

javascript - 简单的跨浏览器绘制javascript库

javascript - 使用 jQuery 的 div 幻灯片

javascript - 带有 key 对对象数组的 ng-options

c# - 通过 HttpClient 向 Saucelabs 发送 PUT 请求没有响应

javascript - React 生命周期事件 - 子组件继承旧状态 Prop

javascript - 如何在 angular.js 中将 div 的所有内容从西类牙语翻译成英语?

javascript - 双向绑定(bind)指令+ Controller

javascript - React API 调用在开发中有效,在生产构建中失败

java - 返回 404 的 Rest Web 服务