0

The Json is in the same repository on the same server than my code. This is not a No 'Access-Control-Allow-Origin' issue -- Why is my post duplicate ?


I am trying to link data from a json file to a js variable.

What is working :

I can manually write and use data like it : This is working very well

...
        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
...

What I want :

Import the data inside the bars variable from a json file.

What is my Json file looking like ?

    {
    "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"
        }
]
}

What I tried :

Attempt N°1 : Using getJSON method

    $.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
                   }]
               });
   });

Attempt N°2 : Using scope and 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

Thank you

Marc_DNL
  • 19
  • 4

0 Answers0