<!DOCTYPE html>
<html>
<body>
<div ng-app="" ng-controller="customersController">
<ul>
<li ng-repeat="x in names">
{{ x.Name + ', ' + x.Country }}
</li>
</ul>
</div>
<script>
function customersController($scope,$http) {
$http.get("http://www.runoob.com/try/angularjs/data/Customers_JSON.php")
.success(function(response) {$scope.names = response;});
}
</script>
<script src="http://apps.bdimg.com/libs/angular.js/1.2.15/angular.min.js"></script>
</body>
</html>
就是這個代碼,想試試$http 獲取json數據來著,但就是無法顯示
困擾好久了
浏覽器的控制審查報錯
已阻止跨源請求:同源策略禁止讀取位於 http://www.runoob.com/try/angularjs/data/Customers_JSON.php 的遠程資源。(原因:CORS 頭缺少 'Access-Control-Allow-Origin')。
如果設置路徑為本地json的話,完全沒有反應
求教啊求教
跨域了,並且 http://www.runoob.com/try/angularjs/data/Customers_JSON.php 這個地址不允許跨域請求,當然會報錯。可以用雅虎的YQL,Yahoo這個地址允許跨域請求,不過注意設置Access-Control-Allow-Origin為*允許跨域,IE跨域對象是XDomaiRequest對象,XMLHttpRequest對象只有IE11才支持
<!DOCTYPE html>
<html>
<body>
<div ng-app="" ng-controller="customersController">
<ul>
<li ng-repeat="x in names">
{{ x.Name + ', ' + x.Country }}
</li>
</ul>
</div>
<script>
function customersController($scope, $http) {
$http({
method:'GET',
url: "http://query.yahooapis.com/v1/public/yql",
params: {
q: "select * from json where url=\"http://www.runoob.com/try/angularjs/data/Customers_JSON.php\"",
format: "json"
}
}).success(function (response) {
console.log(response)
$scope.names = response.query.results.json.json;
});
}
</script>
<script src="http://apps.bdimg.com/libs/angular.js/1.2.15/angular.min.js"></script>
</body>
</html>