gpt4 book ai didi

json - 使用 AngularJS 从 json 检索数据

转载 作者:太空宇宙 更新时间:2023-11-03 22:33:44 24 4
gpt4 key购买 nike

我正在从头开始学习 Angular js,目前正在尝试从 json 文件中检索数据。

我正在使用 -nodejs、express、AngularJS。

之前我在使用时收到错误“Unexpected token D” -

$http.get("/models/driversList.json").success(function(response){$scope.driversList = response})

问题已解决,但现在我用当前代码得到了类似的结果 -

Drivers Championship Standings
1

我猜响应基本上是空白的,因此是“1”,但我不明白为什么会发生这种情况。

以下是我的文件 -

/app.js

app.use('/models',express.static(path.join(__dirname, 'private/data/db/models')));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/scripts',express.static(path.join(__dirname, 'public/javascripts')));
app.use('/styles',express.static(path.join(__dirname, 'public/stylesheets')));
<小时/>

/javascripts/app.js

angular.module('F1FeederApp', [
'F1FeederApp.controllers'
]);
<小时/>

/javascripts/controllers.js

angular.module('F1FeederApp.controllers', []).
controller('driversController', function($scope, $http) {
$http({
method: 'GET',
url: '/models/driversList.json',
data: {},
transformResponse: function (data, headersGetter, status) {
//This was implemented since the REST service is returning a plain/text response
//and angularJS $http module can't parse the response like that.
return {data: data};
}
}).success(function(response){ alert('worked'); $scope.driversList = response}).error(function(response) {
$scope.driversList = [
{
Driver: {
givenName: response,
familyName: 'Vettel'
},
points: 322,
nationality: "German",
Constructors: [
{name: "Red Bull"}
]
},
{
Driver: {
givenName: 'Fernando',
familyName: 'Alonso'
},
points: 207,
nationality: "Spanish",
Constructors: [
{name: "Ferrari"}
]
}
];
})
});
<小时/>

driversList.json

[
{
Driver: {
givenName: 'Eldorado',
familyName: 'Vettel'
},
points: 322,
nationality: "German",
Constructors: [
{name: "Red Bull"}
]
},
{
Driver: {
givenName: 'Fernando',
familyName: 'Alonso'
},
points: 207,
nationality: "Spanish",
Constructors: [
{name: "Ferrari"}
]
}
]
<小时/>

index.html

<!DOCTYPE html>
<html>
<head>
<title>F-1 Feeder</title>
<script src="/scripts/angular.min.js"></script>
<script src="/scripts/controllers.js"></script>
<script src="/scripts/app.js"></script>
</head>

<body ng-app="F1FeederApp" ng-controller="driversController">
<table>
<thead>
<tr><th colspan="4">Drivers Championship Standings</th></tr>
</thead>
<tbody>
<tr ng-repeat="driver in driversList">
<td>{{$index + 1}}</td>
<td>
{{driver.Driver.givenName}}&nbsp;{{driver.Driver.familyName}}
</td>
<td>{{driver.Constructors[0].name}}</td>
<td>{{driver.points}}</td>
</tr>
</tbody>
</table>
</body>
</html>

编辑:

<小时/>

@拉塔纳克

为了回答第 3 点,是的,如果我简单地将数组变量分配给范围变量,它就可以工作。 error() 中的部分是之前工作的部分。

我也尝试了第 1 点,似乎 json 存在于“data”中,因此我将 response.data 分配给 driversList,现在收到此错误 -

Data:
[{Driver:{givenName:'Eldorado',familyName:'Vettel'},points:322,nationality:"German",Constructors:[{name:"Red Bull"}]}]

Error: [ngRepeat:dupes] http://errors.angularjs.org/1.4.7/ngRepeat/dupes?p0=driver%20in%20driversList.data&p1=string%3Ar&p2=r
at Error (native)
at http://localhost:3000/scripts/angular.min.js:6:416
at http://localhost:3000/scripts/angular.min.js:280:39
at Object.fn (http://localhost:3000/scripts/angular.min.js:129:401)
at n.a.$get.n.$digest (http://localhost:3000/scripts/angular.min.js:130:483)
at n.a.$get.n.$apply (http://localhost:3000/scripts/angular.min.js:133:512)
at h (http://localhost:3000/scripts/angular.min.js:87:376)
at K (http://localhost:3000/scripts/angular.min.js:91:499)
at XMLHttpRequest.z.onload (http://localhost:3000/scripts/angular.min.js:93:37)

表示有重复

最佳答案

您的文件 driverlist.json 不包含有效的 JSON 代码。这段代码应该是:

[
{
"Driver": {
"givenName": "Eldorado",
"familyName": "Vettel" },
"points": 322,
"nationality": "German",
"Constructors": [
{"name": "Red Bull"}
]
},
{
"Driver": {
"givenName": "Fernando",
"familyName": "Alonso"
},
"points": 207,
"nationality": "Spanish",
"Constructors": [
{"name": "Ferrari"}
]
}
]

更正此问题并重试。

JSON 总是使用双引号 (") 而不是单引号 (')

有关正确 JSON 的详细信息,请参阅 http://www.json.org/

关于json - 使用 AngularJS 从 json 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33117693/

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