0

I am trying to create light Angularjs app. I am running api server through irb cli and it is running on port 4567. When i run bundle exec ruby.rb I can confirm that there is data on the address: http://localhost:4567/api/v1/companies:

[
  {"id": "1",
   "name": "A"
  },
  {
    "id": "2",
    "name": "B",
  } 
]

I have tried with this code (hello.js):

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
        $scope.company=[];
      $http.get("http://localhost:4567/api/v1/companies")
      /*$http.get("http://localhost/fake.json")*/
    .then(function(response) {
        $scope.company = response.data;

    });
});

HTML:

<html>
    <head>
        <title>Hello AngularJS</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
        <script src="hello.js"></script>
    </head>

        <body>
            <div ng-app="myApp" ng-controller="customersCtrl">
            <ul>
                    <li ng-repeat="x in company">
                        {{ 'name: ' + x.name + ',  id:' + x.id }}
                    </li>
            </ul>
            </div>
        </body>
    </html>

The thing is, if I change the url to the "http://localhost/fake.json" as commented I see the data response clear. But if I use the local web server I can not see anything. Is there some kind of permissions problem or something else.

quant
  • 493
  • 9
  • 21
  • Do you get an error in the console? Or in your backend service logs, – Zooly Nov 15 '17 at 13:27
  • "I am running api server through irb cli" - what do you mean? Since this is tagged ruby-on-rails I´m guessing you are using Rails. You start the rails server with the `bin/rails server` command not through irb. – max Nov 15 '17 at 13:30
  • please see what console has to say about the error and I am not sure but if you see the data in the console we can think of running a digest cycle. But before that see if you have no error in the console in the scenario where you do not see any data on the page. – Deepak Jha Nov 15 '17 at 13:31
  • I did it in Ruby but on Sinatra platform, I have removed the tag – quant Nov 15 '17 at 13:33

2 Answers2

0

use success callback instead of then see this document

$scope.company=[];
      $http.get("http://localhost:4567/api/v1/companies")
      /*$http.get("http://localhost/fake.json")*/
    .success(function(response) {
        $scope.company = response.data;// response or response.data is depend your service response    
    }).error(function(errorStatus)
    {
      //code
    });
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
0

The problem was sinatra-access-control-allow-origin, I found the solution on: Sinatra access-control-allow-origin for sinatra public folder

quant
  • 493
  • 9
  • 21