Javascript $resource:badcfg使用angular调用sharepoint rest服务时
我有一个调用rest服务的旧示例应用程序,它是这样的: 此应用程序不使用angularJSJavascript $resource:badcfg使用angular调用sharepoint rest服务时,javascript,angularjs,rest,sharepoint,Javascript,Angularjs,Rest,Sharepoint,我有一个调用rest服务的旧示例应用程序,它是这样的: 此应用程序不使用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 w
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从sharepoint列表读取数据,但是我正在努力使用$resource.query及其选项
我在这篇文章的标题BADCFG中得到了错误,这意味着我没有使用正确的选项,我没有在文档中找到,如何在代码中设置标题,接受选项
代码如下:
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;
}
}
})
}]
);
}());
ProductResource.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
需要一个数组,而服务器可能不返回数组。可能是因为缺少标题,但在第一个示例中有注释将结果解析为对象,这可能意味着服务器无论如何都不会返回数组。如果是这种情况,您可以添加一个结果转换器。那么您的问题是如何在使用ngResource
时设置标题?是的,我认为这是我的错误,如果您知道如何解释,我将尝试,我希望这就是错误!看起来好多了,你知道我为什么会有这样的反应吗?页面太慢了!它将响应(看起来是html)转换为50000个字符串或类似的数组。我的浏览器甚至没有响应。我将把它作为一个新问题发布,因为badcfg已经不存在了:)@EstebanV看起来响应是一个HTML页面。可能是错误页。@EstebanVvar html='';对于(输入数据){html+=data[key];}console.log(html)代码>
return $resource(url, {}, {
query: { method: 'GET', isArray: true,
headers: { "Accept": "application/json; odata=verbose" }
},
create: { method: 'POST' }
});