gpt4 book ai didi

javascript - Angular.js 和 WebApi 返回 int 数组

转载 作者:行者123 更新时间:2023-11-28 15:47:24 25 4
gpt4 key购买 nike

为了使用 Angular 进行测试,我有一个 Webapi 方法,它返回包含列表的对象的实例。 (json输出如下所示)

现在在我的 Angular Controller 中我想检索这个对象。

有人提示我做错了什么吗?

RestResource 作为工厂

myngApp.factory("valueResource", function ($resource) {

var result =
{
database: $resource("/api/values")
}

return result;
});

Controller :

myngApp.controller("dataController", function ($scope, settings, valueResource) {
$scope.message = "Hello World!";
$scope.userName = settings.user;


$scope.restValues = valueResource.database.query({});
});

json 响应(使用 fiddler 获取)是这样的:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpca2lsblxleHBlcmltZW50YWxcc3JjXG5nMlxXZWJBcHBsaWNhdGlvbjFcV2ViQXBpXHZhbHVlcw==?=
X-Powered-By: ASP.NET
Date: Mon, 17 Feb 2014 14:47:35 GMT
Content-Length: 19

{"Items":[1,2,3,4]}

控制台输出为:

XHR finished loading: "http://localhost:55671/api/values". angular.js:8013
Error: [$resource:badcfg] http://errors.angularjs.org/1.2.12/$resource/badcfg?p0=array&p1=object
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:6:450
at p.then.m.$resolved (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular-resource.min.js:8:517)
at A (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:93:5)
at A (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:93:5)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:94:173
at h.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:102:456)
at h.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:100:218)
at h.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:103:264)
at f (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:67:120) angular.js:9435

最佳答案

查看错误及其文档

http://docs.angularjs.org/error/$resource/badcfg

很明显,查询应该返回一个数组,而您的是一个具有单个属性 Items 的对象。

对资源使用 get 方法。

关于javascript - Angular.js 和 WebApi 返回 int 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21832239/

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