sinat_25564411 于 2015.07.26 00:50 提问
- angularjs,代码完全从w3cschool里扒下来的但是不起作用
-
<!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的话,完全没有反应
求教啊求教
-
-
showbo
2015.07.26 14:05
- 已采纳
跨域了,并且 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>
-
- sinat_25564411 谢谢!
- 2 年多之前 回复
-
- weixin_30117301 2015.07.26 01:19
file和http不是同一个域,最好的是在本地搭一个环境,用Apache之类的
-
- sinat_25564411 搭建好了,然后呢...
- 2 年多之前 回复
-
- sinat_25564411 搭建好了,然后呢...
- 2 年多之前 回复
-
-
caozhy
2015.07.26 05:26
不是本地,而是你应该部署到同一个域下。
-
- sinat_25564411 同一个域=.=
- 2 年多之前 回复
-
- sinat_25564411 怎样部署到同一个5...
- 2 年多之前 回复
-
-
guwei4037
2015.07.26 08:17
跨域了,本地搭建web应用服务器(apache、tomcat、iis之类)。
-
- sinat_25564411 我用的是apache,然后该怎么办
- 2 年多之前 回复
-
- sinat_25564411 我用的是apache,然后该怎么办
- 2 年多之前 回复
-
- suzunshou 2015.07.26 09:25
推荐下个wampserver2.5集成包,在本地搭建一个服务器,然后点击右下角wamp图标,点击www,在里面创建一个文件夹,如:test,把你的代码
文件都放进去 http://www.runoob.com/try/angularjs/data/Customers_JSON.php 修改为http://localhost/test/Customers_JSON.php
-
- sinat_25564411 我用的是apache,但是把本地的放下去没反应
- 2 年多之前 回复