0

Where is the wrong because I did all for this work. Please, someone help me!

<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>  
  <script type="text/javascript">
    var app = angular.module('myApp', []);
    app.controller('heroisCtrl', function($scope, $http) {
       $http.get("https://angularjs.org/greet.php?callback=herois&name=Super%20Hero")
       .success(function(data) { $scope.names = data;});
    });
    </script>
</head>
<body>

<div ng-app="myApp" ng-controller="heroisCtrl">
<table>
  <tr ng-repeat="x in names">
    <td>{{ x.name }}</td>
    <td>{{ x.greeting }}</td>
  </tr>
</table>
</div>

</body>
</html>

At final I have this information by console: XMLHttpRequest cannot load angularjs.org/greet.php?callback=herois&name=Super%20Hero. No 'Access-Control-Allow-Origin' header is present on the requested resource.

  • SO http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource – Jax Jul 10 '15 at 15:01

2 Answers2

1

When fatch data from call back api then must set callback=JSON_CALLBACK

<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>  
  <script type="text/javascript">
        var app = angular.module('myApp', []);
        app.controller('heroisCtrl', function($scope, $http) {
           $http.get("https://angularjs.org/greet.php?callback=JSON_CALLBACK&herois&name=Super%20Hero").success(function(data) { $scope.names = data;});
        });
    </script>
</head>
<body>

    <div ng-app="myApp" ng-controller="heroisCtrl">
        <table>
          <tr ng-repeat="x in names">
            <td>{{ x.name }}</td>
            <td>{{ x.greeting }}</td>
          </tr>
        </table>
    </div>

</body>
</html>
Pang
  • 9,564
  • 146
  • 81
  • 122
Sandeep
  • 1,461
  • 13
  • 26
  • Perfect Sandeep, but, How do I when need use other API, for example this: http://www.folhacar.com.br/frontendnovo.php/api/listMarcas?callback=JSON_CALLBACK Is not work. I'm using {{ x.nome_marca }} for print, but... is not working. – Fábio Duarte Jul 10 '15 at 20:17
  • It's so simple... follow this code: `$http.get("http://www.folhacar.com.br/frontendnovo.php/api/listMarcas?callback=JSON_CALLBACK").success(function(data) { $scope.names = data;});` – Sandeep Jul 11 '15 at 17:04
0

this problem is your backend! You have to add

 AllowOrigin = '*' //not secure

Or add

AllowOrigin: 'yourClientAddressOrIp'

Not sure about sintax, but the idea is as i write before, then should be there. Sintax depends on your backend languaje.

And thats All

Regards

Max Pinto
  • 1,463
  • 3
  • 16
  • 29