javascript - 使用来自 AngularJS 的 Web API JSON 响应 - 错误 : expected and array but got an object

标签 javascript angularjs asp.net-web-api

我有一个 Web API,它以 JSON 格式返回响应,格式如下:

{
  "email": "john@google.com",
  "password": null,
  "accessLevel": 2
}

我正在尝试访问该响应中的 accessLevel 字段,但收到此 Angular 错误:

Error in resource configuration for action `query`. Expected response to contain an array but got an object (Request: GET http://localhost:51608/api/UserRole?email=john@google.com...)

这是我的 Angular 资源代码(如下),我刚刚添加了 isArray false 来尝试解决问题:

function userRoleResource($resource, appSettings) {
    return $resource(appSettings.serverPath + "/api/UserRole?email=:email", { email: '@email' },
    {
        get: {
            method: 'GET',
            isArray: false
        }
    });
}

这就是我尝试使用数据的方式:

userRoleResource.query({ email: vm.userData.email },
   function (data) {
      vm.userData.accessLevel = data.AccessLevel;
});

最佳答案

您指定“get”函数不是数组,但您正在使用“query”函数。

试试这个:

userRoleResource.get({ email: vm.userData.email },
    function (data) {
        vm.userData.accessLevel = data.AccessLevel;
});

关于javascript - 使用来自 AngularJS 的 Web API JSON 响应 - 错误 : expected and array but got an object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36728069/

相关文章:

javascript - 基本 onclick javascript jquery 代码到 Vue

javascript - 正在传递 ng-click 中必需的 $event

javascript - 来自模板的 Angular 修改指令

angularjs - 在 Web API 2 中如何接受简单类型作为 post 参数以及 Route 参数

odata - 使用 Breezejs 发布批量请求

c# - 如果实现接口(interface)的类将使用 List<T>,为什么还要在接口(interface)定义中使用 IEnumerable<T>?

javascript - 为什么 Cypress 在运行 get 命令后说我的元素已分离?

javascript - html5 视频 "ended"事件在 chrome 和 IE 中不起作用

javascript - 检查高度,然后添加或删除类

angularjs - 如何查询 JSON 对象