- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个旧的示例应用程序,它调用休息服务,其内容如下:此应用程序不使用 angularJS。
var listName = "Events";
// the url to use for the REST call.
var url = SPAppWebUrl + "/_api/SP.AppContextSite(@target)" +
// this is the location of the item in the parent web. This is the line
// you would need to change to add filters, query the site etc
// "/web/lists/getbytitle('" + listName + "')/items?" +
"/web/lists/getbytitle('" + listName + "')/items?$select=Title,Category,EventDate,Description,EncodedAbsUrl,ID" +
"&@target='" + SPHostUrl + "'";
// create new executor passing it the url created previously
var executor = new SP.RequestExecutor(SPAppWebUrl);
// execute the request, this is similar although not the same as a standard AJAX request
executor.executeAsync(
{
url: url,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
// parse the results into an object that you can use within javascript
var results = JSON.parse(data.body);
var events = [];
问题出在我的新应用程序中,我使用 Angular 创建了一个应用程序,该应用程序使用 Rest 从共享点列表中读取数据,但是我正在努力解决 $resource.query 及其选项。
我在这篇文章的标题中收到错误,BADCFG,这意味着我没有使用正确的选项,我在文档中没有找到如何在我的代码中设置 header 、接受选项。
代码如下:
App.Js
var SPHostUrl;
var SPAppWebUrl;
var ready = false;
$(document).ready(function () {
var params = document.URL.split("?")[1].split("&");
for (var i = 0; i < params.length; i = i + 1) {
var param = params[i].split("=");
switch (param[0]) {
case "SPAppWebUrl":
SPAppWebUrl = decodeURIComponent(param[1]);
break;
case "SPHostUrl":
SPHostUrl = decodeURIComponent(param[1]);
break;
}
}
});
(function () {
"use strict";
var app = angular.module("productManagement",
["common.services",
"ui.router",
"ui.mask",
"ui.bootstrap"]);
app.config(["$stateProvider",
"$urlRouterProvider",
function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/products");
$stateProvider
.state("home", {
url: "/",
templateUrl: "../Scripts/app/welcomeView.html"
})
// Products
.state("productList", {
url: "/products",
templateUrl: "../Scripts/app/products/productListView.html",
controller: "ProductListCtrl as vm"
})
.state("productEdit", {
abstract: true,
url: "/products/edit/:productId",
templateUrl: "../Scripts/app/products/productEditView.html",
controller: "ProductEditCtrl as vm",
resolve: {
productResource: "productResource",
product: function (productResource, $stateParams) {
var productId = $stateParams.productId;
return productResource.get({ productId: productId }).$promise;
}
}
})
.state("productEdit.info", {
url: "/info",
templateUrl: "../Scripts/app/products/productEditInfoView.html"
})
.state("productEdit.price", {
url: "/price",
templateUrl: "../Scripts/app/products/productEditPriceView.html"
})
.state("productEdit.tags", {
url: "/tags",
templateUrl: "../Scripts/app/products/productEditTagsView.html"
})
.state("productDetail", {
url: "/products/:productId",
templateUrl: "../Scripts/app/products/productDetailView.html",
controller: "ProductDetailCtrl as vm",
resolve: {
productResource: "productResource",
product: function (productResource, $stateParams) {
var productId = $stateParams.productId;
return productResource.get({ productId: productId }).$promise;
}
}
})
}]
);
}());
ProductListrCtrl.js
(function () {
"use strict";
angular
.module("productManagement")
.controller("ProductListCtrl",
["productResource",
ProductListCtrl]);
function ProductListCtrl(productResource) {
var vm = this;
productResource.query(function (data) {
vm.products = data;
});
vm.showImage = false;
vm.toggleImage = function () {
vm.showImage = !vm.showImage;
}
}
}());
产品资源.js
(function () {
"use strict";
angular
.module("common.services")
.factory("productResource",
["$resource",
productResource]);
function productResource($resource) {
var listName = "Products";
// the url to use for the REST call.
var url = SPAppWebUrl + "/_api/SP.AppContextSite(@target)" +
// this is the location of the item in the parent web. This is the line
// you would need to change to add filters, query the site etc
// "/web/lists/getbytitle('" + listName + "')/items?" +
"/web/lists/getbytitle('" + listName + "')/items?$select=Id,productName,productCode,releaseDate,description,cost,price,category,tags,imageUrl" +
"&@target='" + SPHostUrl + "'";
//return $resource(url);
//return $resource("/api/products/:productId")
return $resource(url, {}, {
query: { method: 'GET', isArray: true },
create: { method: 'POST' }
})
}
}());
最佳答案
至于添加标题:
return $resource(url, {}, {
query: { method: 'GET', isArray: true,
headers: { "Accept": "application/json; odata=verbose" }
},
create: { method: 'POST' }
});
但是这个错误意味着不同的东西。 query
需要一个数组,而服务器可能不会返回一个数组。也许是因为缺少 header ,但在第一个示例中,有注释将结果解析为对象
,这可能意味着服务器无论如何都不会返回数组。如果是这种情况,您可以添加结果转换器。
关于javascript - $资源:badcfg when calling sharepoint rest service using angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26934602/
这是我的js代码 var MYModule = angular.module('myApp', ['ngRoute', 'myAppServices']) .directive('loadin
我有一个 API,可以根据给定的 ID 将数据返回到 AngularJS,但是,当数据以 JSON 形式返回时,AngularJS 会抛出“badcfg”错误,据我所知,这是由于返回数据,但不知道如何
我的 Angular 抛出了我 Error: [$resource:badcfg] Error in resource configuration for action query. Expected
我正在尝试将spotify api与node js和Angular js连接。每当我尝试获取特定艺术家的详细信息时,我都会收到此错误。 app.js var app = angular.module
我有一个为我发布数据的服务模块: //services.js var myServices = angular.module('myServices', ['ngResource']); myServ
我已查看此处的其他帖子,但仍无法解决我的问题。 我得到一个错误: $resource:badcfg Response does not match configured parameter 我相信这个
我是一个完整的 angular 菜鸟,只是通过了一个代码学校教程,我已经遇到了我的第一个障碍。 我收到 Error: [$resource:badcfg] Error in resource conf
我有一个旧的示例应用程序,它调用休息服务,其内容如下:此应用程序不使用 angularJS。 var listName = "Events"; // the url to use fo
如何修复错误: [$resource:badcfg] Error in resource configuration. Expected response to contain an array bu
测试我的 REST 服务时,我收到以下错误消息: Error: [$resource:badcfg] query http://errors.angularjs.org/1.4.6/$resource
我想使用 $resources 从 json 文件中获取数据,但我总是遇到该错误。但是,我可以使用 $http 来完成,因此我认为我的服务设置有问题。 这是我的 Controller : $scope
我是 MEAN 堆栈开发的新手,昨天才开始。我正在尝试通过使用链接到服务器端 Controller 的资源进行调用,从数据库中取回我的数据。但我收到以下控制台错误“错误:[$resource:badc
我正在尝试使用路由参数来过滤 REST 调用的结果,但没有得到任何返回结果,而是收到以下错误: Error: error:badcfg Response does not match configur
以下是我设置文章资源的方式: define(['services/index', 'services/Helpers'], function(services) { 'use strict';
我是一名优秀的程序员,十分优秀!