0
votes

I am getting this error. XMLHttpRequest cannot load http://communityempowerment.org.pk/test-sample/api/get_category_index/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

my code is

.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
You can't do ajax requests to random cross-origin resources, it violates the same-origin policy. The only workaround is to control the server and add the correct headers, return JSONP, or use your own server to make the request. - adeneo

2 Answers

1
votes

The server that is serving http://communityempowerment.org.pk/test-sample/api/get_category_index/ doesn't allow cross-origin requests. Take a look at CORS on MDN. If you control the server, you can change the header Access-Control-Allow-Origin to either a specific set of domains, or a wildcard * to allow access from all origins.

0
votes

Your code is correct the problem is coming from the server.You should enable cross-origin. http://enable-cors.org/server.html