0

I know there are tons of information about CORS but there are so many that I can't find what I need. I'm building a Cordova APP and I'm calling Google Maps Distance API to get the distance between two points. But when I called it using $resourse I get CORS error.

error:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.

My factory

function googleMapsApi($resource, API_KEY_WIN,DIST_URL_WIN){

        var url = DIST_URL_WIN+mylat+','+mylng+'&destinations='+lat+','+lng+'&key='+API_KEY_WIN;
            console.log(url);
            var result = $resource(url, {}, {get: {
                method: 'GET',
                headers: {
                    'Access-Control-Allow-Origin': '*',
                    'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, DELETE, PUT',
                    'Access-Control-Allow-Headers': 'Origin, Content-Type, Accept, Authorization, X-Request-With, X-CLIENT-ID, X-CLIENT-SECRET',
                    'Access-Control-Allow-Credentials': true
                }
                }
            });
            return result;
}

What am I doing wrong? Note: Installing Chrome's plugin won't solve my problem.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Ezequiel Ramiro
  • 760
  • 1
  • 12
  • 29

1 Answers1

0

The Access-Control-Allow headers should emitted by the server itself, not the requesting client. I think you will need to use a Google API pack for this, served from the Google domain.

<script src="https://apis.google.com/js/api.js" type="text/javascript"></script>
  • I need to calculate from my phone's location what is the distance of several restaurants, if I do that from the server, will take to much processing, think that 1000 users request at the same time. Is it really that the way to do it? – Ezequiel Ramiro Mar 22 '18 at 21:37