-1

I am giving API call to google map API and its throwing cors error. such as:- No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin

code snippet

$.getJSON(
  "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" +
    city_lat + "," + city_lon + "&radius=500&type=restaurant&key=AIzaSyC0Lytb6QPHW9NNMwfJva6U4meM9tnvtlk",
  function(val) { }
);
31piy
  • 23,323
  • 6
  • 47
  • 67
user9485480
  • 11
  • 1
  • 2

1 Answers1

0

You will need to change your Cross Origin Policy.

Regular web pages can use the XMLHttpRequest object to send and receive data from remote servers, but they're limited by the same origin policy.

See this for more information: How to get a cross-origin resource sharing (CORS) post request working

If you are using localhost via Chrome, see this:

To have Chrome send Access-Control-Allow-Origin in the header, just alias your localhost in your /etc/hosts file to some other domain, like:

127.0.0.1 localhost yourdomain.com

Then if you'd access your script using yourdomain.com instead of localhost, the call should succeed

Rence
  • 2,900
  • 2
  • 23
  • 40
  • I have used following code but it is also not working:- var xhr = new XMLHttpRequest(); xhr.open("POST", "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+city_lat+","+city_lon+"&radius=500&type=restaurant&key= ); – user9485480 Apr 18 '18 at 04:58
  • Have you adjusted the Cross Origin as described in the links? Without it you can not request rescources from a different server. See this as well: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS – Rence Apr 18 '18 at 07:15