我收到了这个错误 . XMLHttpRequest无法加载http://communityempowerment.org.pk/test-sample/api/get_category_index/ . 请求的资源上没有'Access-Control-Allow-Origin'标头 . 因此不允许原点'http://localhost:8100'访问 .
我的代码是
.controller('MenuCtrl', function($http, $scope){
$scope.categories = [];
$http.get("http://communityempowerment.org.pk/test-sample/api/get_category_index/").then
(function(data){
$scope.categories = data.data;
console.log(data);
}, function(err){
console.log(err);
})
})
2 回答
您的代码是正确的,问题来自服务器 . 您应该启用跨源 . http://enable-cors.org/server.html
为
http://communityempowerment.org.pk/test-sample/api/get_category_index/
提供服务的服务器不允许跨源请求 . 看看CORS on MDN . 如果您控制服务器,则可以将 HeadersAccess-Control-Allow-Origin
更改为一组特定的域,或通配符*
以允许从所有源进行访问 .