gpt4 book ai didi

javascript - 过滤 AngularJS REST JSON 数据 : error:badcfg Response does not match configured parameter

转载 作者:行者123 更新时间:2023-11-30 17:07:03 25 4
gpt4 key购买 nike

我正在尝试使用路由参数来过滤 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

我尝试获取的值是文章类别 ID。如果我将我的 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' }
}
);
}]);

这是我的 Controller :

 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="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>

以下是我如何链接到此页面以通过路由参数 (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 的路由参数也是如此。所以我专注于失败调用的 ?$filter= 方面。其他人是否有将值传递到 REST 调用的问题,因为变量不是直接在/之后,而是在/:articleID

最佳答案

在你的 pfcServices 工厂定义中添加 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=} – 如果为真,则此操作返回的对象是一个数组,请参阅返回部分。

参见文档 here

关于javascript - 过滤 AngularJS REST JSON 数据 : error:badcfg Response does not match configured parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27788801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com