2015-07-10 63 views
0

錯在哪裏,因爲我爲這項工作做了所有工作。請有人幫助我!如何解決使用angularJS時獲取JSON數據的問題

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

在最後我不得不通過控制檯的信息:XMLHttpRequest的無法加載angularjs.org/greet.php?callback=herois &名=超級%20Hero。請求的資源上沒有「Access-Control-Allow-Origin」標題。

+0

SO http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-資源 – Jax

回答

0

混帳數據這個問題是你的後臺! 您必須添加

AllowOrigin = '*' //not secure 

或者添加

AllowOrigin: 'yourClientAddressOrIp' 

不確定sintax,但這個想法是因爲我之前寫的,那麼應該有。 Sintax取決於您的後端語言。

和多數民衆所有

問候

1

當從回調API則必須設置回調= 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> 
+0

Perfect Sandeep,但是,我該如何在需要使用其他API時,例如:http://www.folhacar.com.br/frontendnovo.php/api/listMarcas?callback=JSON_CALLBACK 不行。我正在使用{{x.nome_marca}}進行打印,但是...無法正常工作。 –

+0

這很簡單...遵循以下代碼:'$ http.get(「http://www.folhacar.com.br/frontendnovo.php/api/listMarcas?callback=JSON_CALLBACK」).success(function(data) {$ scope.names = data;});' – Sandeep

相關問題