Json与我的代码位于同一服务器上的同一个存储库中。这不是No'Access-Control-Allow-Origin'问题 - 为什么我的帖子会重复?
我正在尝试将json文件中的数据链接到js变量。
我可以手动编写和使用类似的数据: 这非常有效
...
var bars = [
{
lat: 48.11300540000001,
lng: -1.6825228000000152,
name: 'Le Kenland',
demi: 2
},
{
lat: 48.11300540000001,
lng: -1.6825228000000152,
name: 'Le Petit Velo',
demi: 3
}
]
...
// And then use it like it :
...
var title = bars[i].name;
// This is working very well
...
从json文件中导入bars变量中的数据。
{
"bars": [{
"id": "1",
"name": "Le Kenland",
"description": null,
"lat": "48.11300540000001",
"lng": "-1.6825228000000152",
"type": "bar",
"phone": "+33 2 99 79 02 02",
"demi": "3",
"pinte": "5",
"open": "11:00",
"closed": "01:00"
},
{
"id": "2",
"name": "Le Combi Bar",
"description": null,
"lat": "48.1177456",
"lng": "-1.6853853000000072",
"type": "bar",
"phone": "+33 2 99 30 50 19",
"demi": "2",
"pinte": "5",
"open": "10:00",
"closed": "23:00"
}
]
}
尝试N°1:使用getJSON方法
$.getJSON('https://pdl.bourri.co/keosu/web/bars.json', function(data) {
$.each( data.bars, function(i, value) {
var bars = [{
lat: value.lat,
lng: value.lng,
name: value.name,
demi: value.demi
}]
});
});
尝试N°2:使用范围和http.get
$scope.init = function (params) {$scope.getBars()}
$scope.barsDataUrl = "https://pdl.bourri.co/keosu/web/bars.json";
$scope.getBars = function(){
$http.get($scope.barsDataUrl)
.success(function (data) {
$scope.bars = data.bars;
$scope.bar = $scope.bars.lat;
$scope.bar = $scope.bars.lng;
$scope.bar = $scope.bars.name;
$scope.bar = $scope.bars.demi;
});
}
var bars = [{
lat: bar.lat,
lng: bar.lng,
name: bar.name,
demi: bar.demi
}]
// and several other attempt modifying the bars var
谢谢