<!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>
file和http不是同一个域,最好的是在本地搭一个环境,用Apache之类的
不是本地,而是你应该部署到同一个域下。
跨域了,本地搭建web应用服务器(apache、tomcat、iis之类)。
推荐下个wampserver2.5集成包,在本地搭建一个服务器,然后点击右下角wamp图标,点击www,在里面创建一个文件夹,如:test,把你的代码
文件都放进去 http://www.runoob.com/try/angularjs/data/Customers_JSON.php 修改为http://localhost/test/Customers_JSON.php