0

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);
    })

})
1
  • 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. Commented Mar 5, 2016 at 5:28

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.