Javascript 筛选器AngularJS REST JSON数据:错误:badcfg响应与配置的参数不匹配
我试图使用routeparam来过滤REST调用的结果,但没有得到任何返回的结果,而是收到以下错误:Javascript 筛选器AngularJS REST JSON数据:错误:badcfg响应与配置的参数不匹配,javascript,angularjs,rest,Javascript,Angularjs,Rest,我试图使用routeparam来过滤REST调用的结果,但没有得到任何返回的结果,而是收到以下错误: 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确
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确定的。如果我将我的URL放入浏览器工具(如Postman)中,它就会工作。articlecategoryid根据需要返回的URL示例如下:
https://myrestcall.net/tables/articles/?$filter=(articlecategoryid eq 1)
但是,在Angular中使用此选项似乎无法解决问题并产生错误
以下是此调用中的一些示例REST数据:
[
{
"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. "
},
]
以下是我的应用程序设置:
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 });
}]);
下面是将输出REST数据(categories.html)的页面:
div class=“行”>
标题
排序方式:
身份证件
文章标题
物品类别
身份证件
标题
类别ID
{{articles.id}
{{articles.articletitle}
{{articles.articlescategoryid}
下面是我如何链接到该页面以通过routeparam(home.html)进行过滤的:
更新:我的其他REST调用正在工作,即使使用routeparam,例如,我将重点放在失败调用的?$filter=方面。是否有其他人在将值传递到REST调用时遇到问题,因为变量不是直接在您的
pfcServices
factory定义中的/a/后面,因为它在/:articleID中添加isArray:true
到'update':{method:'PATCH'}
,每次得到JSON数组时,都需要将isArray:true
添加到方法定义中
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', isArray:true }
}
);
}]);
isArray
–{boolean=}–如果为true,则此操作返回的对象是数组,请参阅“返回”部分
请参阅文档解决了它!问题出在控制器中,因为我正在使用“Get”。“Get”可以通过id(例如)检索某些内容 但是,我需要使用'query'命令,因为它有一个隐式的isArray:true(请参见其中的'query':{method:'GET',isArray:true},) 当我从以下位置更改控制器时:
pfcControllers.controller('pfcCategoriesCtrl', ['$scope', '$routeParams', 'pfcArticleCategories', function ($scope, $routeParams, pfcArticleCategories) {
$scope.category = pfcArticleCategories.get({ articlecategoryID: $routeParams.articlecategoryID });
}]);
致:
它可以根据需要进行解析。这是否包括“获取”操作,因为我只为更新定义了一个方法,但此时只使用了“获取”。我添加了isArray:true,但它仍然给出了错误。当我将REST调用硬编码到?$filter=(articlecategoryid eq 1)时,它会给出结果,但当使用?$filter=(articlecategory eq:articlecategoryid)时,它会失败并给出错误,即使在调试器中它正确地传递变量并将REST调用设置为?$filter=(articlecategoryid eq 1)。嗯,下面是它的重点。你确定你没有在其他地方发现错误吗?另外,我有一个发出CORS请求的应用程序,我必须添加这一行
delete$httpProvider.defaults.headers.common[“X-Requested-With”];//删除X-request-With
,同时尝试注释$httpProvider.defaults.headers.common['Content-Type']='Application/json'代码>也许那是马车。让我知道它是如何工作的哦,我想这就是问题所在,我不认为它是URL编码的。尝试在URL定义中使用(在return$resource('https://myrestcall.net/tables/articles/?$filter(articlecategoryid等式:articlecategoryid)
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', isArray:true }
}
);
}]);
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 });
}]);