0

Ive been beating myself over my head with this question and I've gone through a lot of documents online however none of them mentions how to create a header file. I know that that we implement Access-Control-Allow-Origin: * But I don't know where to implement it.

I'm trying to access a Json file in my local host Apache server, from get request in angular1.

i'm trying to acces the file however I keep getting cross origin sharing problem.

Could anyone answer where we implement the access control allow origin, The sources from internet say we implement it on the header file but where does the header file lie

app.controller("myCtrl", 

    function($scope,$http) 
    {

              alert("This is finally working!!");


      return $http.get('http://localhost/week10/resources/dog.json')
      .then(
            function(response)
            {
              $scope.dogObj = response.data;
            })
    });         

My code looks something like this. Any help would be hugely appreciated!!!

  • The `Access-Control-Allow-Origin` header is set on the server, not from your app. I don't know how to set that in Apache (we use IIS). That being said, I'm extremely surprised you are getting CORS errors on localhost. – Lex May 24 '18 at 14:35
  • Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost/week10/resources/dog.json. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Thats the error I'm getting; Ive tried to use the object on the file location and instead of using local host use the location and it works just fine this doesn't work at all – Binay Dhawa May 24 '18 at 14:46
  • Weird. Anyway, you have to change the setting on your server because it's a protection to keep other domains from calling APIs on your server. If you could override this on the client then it would defeat the purpose of protecting against CORS which is why you have to explicitly allow it on your server. – Lex May 24 '18 at 14:56
  • Hi, have you checked this post? https://stackoverflow.com/questions/23823010/how-to-enable-cors-in-angularjs ? I checked someone points out the following url which might have useful information https://enable-cors.org/server.html – eduardo92 May 24 '18 at 16:03

1 Answers1

1

This is a server side issue , since front end and backend application are running in two different servers , you need to add CORS filter in server side Can i know how are you calling the json file , apachi tomcat ?

In front end : allow cross origin

Vignesh
  • 81
  • 3