Skip to content

Unable to access google maps api due to CORS #6389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hugo-dlb opened this issue May 19, 2017 · 10 comments
Closed

Unable to access google maps api due to CORS #6389

hugo-dlb opened this issue May 19, 2017 · 10 comments

Comments

@hugo-dlb
Copy link

hugo-dlb commented May 19, 2017

Bug Report or Feature Request (mark with an x)

- [x ] bug report
- [ ] feature request

Versions.

@angular/cli: 1.0.4
node: 6.10.2
os: win32 x64
@angular/animations: 4.1.1
@angular/common: 4.1.1
@angular/compiler: 4.1.1
@angular/compiler-cli: 4.1.1
@angular/core: 4.1.1
@angular/forms: 4.1.1
@angular/http: 4.1.1
@angular/platform-browser: 4.1.1
@angular/platform-browser-dynamic: 4.1.1
@angular/platform-server: 4.1.1
@angular/router: 4.1.1
@angular/cli: 1.0.4

Repro steps.

const serviceURL = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=' + address + '&key=MyAPIKey'; return this._http.get(serviceURL) .map((response: Response) => response.json()) .catch(error => { console.log(error); return Observable.throw('error'); });

The log given by the failure.

XMLHttpRequest cannot load https://maps.googleapis.com/maps/api/place/textsearch/json?query=16%20B%20R…0%20PARIS%2020%20Ile-de-France&key=... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

Desired functionality.

The header 'Access-Control-Allow-Origin' added in #2872 does not seem to work.
MRHarrison@8d50a27

Mention any other details that might be useful.

I'm having this problem on any browser, and i have to use a Google chrome extention to get rid of it.
https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi
I tried changing my @angular/cli version in the devDependencies to use the version 1.0.4, but nothing changes.
I printed the serviceURL to try it in postman and in my browser and i don't have any errors, the json response from the api shows up perfectly.
You can get a free API key here if you want to try : https://developers.google.com/places/web-service/get-api-key?hl=fr

@derekkite
Copy link

@hugo-dlb
Copy link
Author

Your first link says 'There isn't anything the client application can do about these errors. The server must be configured to accept the application's requests.'
And your second link says to do a basic ajax request
var xhr = new XMLHttpRequest(); xhr.open('GET', 'https://people.googleapis.com/v1/people/me/connections?sortOrder=LAST_NAME_ASCENDING');
Which, i believe, is what is used by Angular 2 when doing http.get.
What am i doing wrong?

@Brocco
Copy link
Contributor

Brocco commented May 22, 2017

This is a support question, not necessarily a bug with the CLI, I suggest opening a question on a site like https://stackoverflow.com/ to get some assistance.

@Brocco Brocco closed this as completed May 22, 2017
@navjotbola
Copy link

Were you able to solve this issue?

@hugo-dlb
Copy link
Author

hugo-dlb commented Sep 8, 2017

@farazsarwar113
Copy link

Having the same issue !

@rgantla
Copy link

rgantla commented Dec 21, 2017

In my experience the plugins worked with http but not with the latest httpClient. Also, configuring the CORS respsonse headers on the server wasn't really an option. So, I created a proxy.conf.json file to act as a proxy server.

Read more about this here: https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md

below is my prox.conf.json file

{
  "/posts": {
    "target": "https://example.com",
    "secure": true,
    "pathRewrite": {
    "^/posts": ""
  },
    "changeOrigin": true
  }
}

I placed the proxy.conf.json file right next the the package.json file in the same directory

then I modified the start command in the package.json file like below

"start": "ng serve --proxy-config proxy.conf.json"

now, the http call from my app component is as follows

return this._http.get('/posts/pictures?method=GetPictures')
.subscribe((returnedStuff) => {
  console.log(returnedStuff);
});

Lastly to run my app, I'd have to use npm start or ng serve --proxy-config proxy.conf.json

@zaarheed
Copy link

I've spent the last few days trying to find the cleanest solution for this with not much luck. Google do offer a client-side JavaScript library. But to use it, you have to hard-code a script file in index.html which I totally disagree with (think: what if the device doesn't have an internet connection, how would you handle errors?)

I also found that, Google have deprecated JSONP after moving from v2 of the API to v3 which sucks because that would have been the ideal work-around.

Instead, the only real solution I could find was to set up a super-simple NodeJS/Express backend which is configured correctly for CORS and acts as a proxy between your Ionic application and the Google API.

It was the first time I ever tried Node and it wasn't too difficult to set this up. I wrote a guide here. and also created a sample Git repo here.

@joshua-holly89
Copy link

joshua-holly89 commented Dec 11, 2018

Still facing the same problem, one year later.
Is there a clean Angular now without this dirty solution from?
https://developers.google.com/maps/documentation/javascript/places#find_place_from_query

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants