过滤AngularJS REST JSON数据:错误:badcfg响应不匹配的参数配置 [英] Filter AngularJS REST JSON Data: error:badcfg Response does not match configured parameter
问题描述
我试图用一个routeparam过滤从REST调用的结果,但我没有得到任何结果返回,而是收到以下错误:
I am trying to use a routeparam to filter the results from a REST call, but am not getting any results returned but rather receiving the following error:
Error: error:badcfg Response does not match configured parameter
Error in resource configuration for action `get`. Expected response to contain an object but got an array
我试图得到的值是由articlecategoryid。如果我把我的网址到浏览器的工具,如邮差它的作品。返回由articlecategoryid根据需要一个例子网址如下:
The value I am attempting to get is by the articlecategoryid. If I drop my URL into a browser tool such as Postman it works. An example URL that returns as desired by articlecategoryid is as follows:
https://myrestcall.net/tables/articles/?$filter=(articlecategoryid eq 1)
但是,使用此角中似乎没有解决,并产生错误。
However, using this in Angular does not seem to resolve and produces the error.
下面是这个调用一些样本REST数据:
Here is some sample REST Data from this call:
[
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D4",
"articletitle": "artilce1",
"articlecategoryid": 1,
"articlesummary": "article 1 summary. "
},
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D5",
"articletitle": "artilce2",
"articlecategoryid": 1,
"articlesummary": "article 2 summary. "
},
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D6",
"articletitle": "artilce3",
"articlecategoryid": 1,
"articlesummary": "article 3 summary. "
},
]
下面是我的应用程序设置:
Here is my App setup:
var pfcModule = angular.module('pfcModule', [
'ngRoute',
'ui.bootstrap',
'auth0',
'angular-storage',
'angular-jwt',
'pfcServices',
'pfcControllers']);
pfcModule.config([
'$routeProvider',
'authProvider',
'$httpProvider',
'$locationProvider',
'jwtInterceptorProvider',
function ($routeProvider, authProvider, $httpProvider, $locationProvider, jwtInterceptorProvider) {
$routeProvider.
when('/home', { templateUrl: './views/home.html' }).
when('/categories/:articlecategoryID', { templateUrl: './views/categories.html', controller: 'pfcCategoriesCtrl' }).
when('/article/:articleID/:articleTitle', { templateUrl: './views/article.html', controller: 'pfcCtrl2' }).
when('/add-article', { templateUrl: './views/add-article.html', controller: 'pfcPost', requiresLogin: true }).
when('/login', { templateUrl: './views/login.html', controller: 'loginCtrl' }).
otherwise({ redirectTo: '/home' });
$httpProvider.defaults.headers.common['X-ZUMO-APPLICATION'] = 'myid';
$httpProvider.defaults.headers.common['Content-Type'] = 'Application/json';
authProvider.init({
domain: 'mydomain',
clientID: 'myid',
callbackURL: location.href,
loginUrl: '/login'
});
jwtInterceptorProvider.tokenGetter = function (store) {
return store.get('token');
}
$httpProvider.interceptors.push('jwtInterceptor');
}])
.run(function ($rootScope, auth, store, jwtHelper, $location) {
$rootScope.$on('$locationChangeStart', function () {
if (!auth.isAuthenticated) {
var token = store.get('token');
if (token) {
if (!jwtHelper.isTokenExpired(token)) {
auth.authenticate(store.get('profile'), token);
} else {
$location.path('/login');
}
}
}
});
});
下面是我的服务:
var pfcServices = angular.module('pfcServices', ['ngResource'])
pfcServices.factory('pfcArticleCategories', ['$resource', function ($resource) {
return $resource('https://myrestcall.net/tables/articles/?$filter=(articlecategoryid eq :articlecategoryID)', { articlecategoryID: '@articlecategoryid' },
{
'update': { method: 'PATCH' }
}
);
}]);
下面是我的控制器:
var pfcControllers = angular.module('pfcControllers', ['auth0']);
pfcControllers.controller('pfcCategoriesCtrl', ['$scope', '$routeParams', 'pfcArticleCategories', function ($scope, $routeParams, pfcArticleCategories) {
$scope.category = pfcArticleCategories.get({ articlecategoryID: $routeParams.articlecategoryID });
}]);
下面是网页,将输出静态数据(categories.html):
Here is the page that would output the REST data (categories.html):
div class="row">
<div class="col-md-4">
<h2>Heading</h2>
Sort By: <select ng-model="articleSortOrder">
<option value="+id">ID</option>
<option value="+articletitle">Article Title</option>
<option value="+articlecategoryid">Article Category</option>
</select>
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Title</th>
<th>Category ID</th>
</tr>
<tr ng-repeat="articles in category | orderBy:articleSortOrder">
<td>{{articles.id}}</td>
<td>{{articles.articletitle}}</td>
<td>{{articles.articlecategoryid}}</td>
</tr>
</table>
</div>
下面是我如何链接到这个网页的routeparam(home.html做为)对其进行过滤:
Here is how I link into this page to filter it by the routeparam (home.html):
<a href="#categories/1">Article 1 Category</a>
<a href="#categories/2">Article 2 Category</a>
更新:我的其他REST调用如的 https://myrestcall.net/tables/articles/:articleID 所以我专注于?$ =过滤器的调用失败的一个方面。有其他人有问题,与传递值到一个REST调用,因为变量后,不能直接/因为它是在/:条款ArticleID
UPDATE: My other REST calls are working, even with a routeparam such as https://myrestcall.net/tables/articles/:articleID so I am focusing on the ?$filter= aspect of the failing call. Have others had issues with passing values into a REST call, as the variable is not directly after a / as it is in /:articleID
推荐答案
解决它!问题是在控制器我使用的是GET。 A'获取'工作正常的ID(如检索的东西。 https://myrestcall.net/tables /文章/。条款ArticleID)
Solved it! The issue is in the controller as I was using a 'Get'. A 'Get' works fine for retrieving something by the id (ex. https://myrestcall.net/tables/articles/:articleID).
不过,我需要使用'查询'命令,因为这有一个隐IsArray的:真(请参阅角资源文件在声明'查询':{方法:GET,IsArray的:真正})
However, I needed to use the 'query' command as this has an implicit isArray:true (please see the Angular Resource Doc where it states 'query': {method:'GET', isArray:true},)
当我从改变了我的控制器:
When I changed my controller from:
pfcControllers.controller('pfcCategoriesCtrl', ['$scope', '$routeParams', 'pfcArticleCategories', function ($scope, $routeParams, pfcArticleCategories) {
$scope.category = pfcArticleCategories.get({ articlecategoryID: $routeParams.articlecategoryID });
}]);
要:
pfcControllers.controller('pfcCategoriesCtrl', ['$scope', '$routeParams', 'pfcArticleCategories', function ($scope, $routeParams, pfcArticleCategories) {
$scope.category = pfcArticleCategories.query({ articlecategoryID: $routeParams.articlecategoryID });
}]);
它解决了应有的作用。
It resolves as desired.
这篇关于过滤AngularJS REST JSON数据:错误:badcfg响应不匹配的参数配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!