G
G
GaserV2017-06-05 23:57:51
JavaScript
GaserV, 2017-06-05 23:57:51

Why is it throwing CORS error google api autocomplete?

Such situation. I want to get a list of cities where value = "Mo" for example. Code below. In the first case, it gives an error: Uncaught SyntaxError: Unexpected token :

const url = `https://maps.googleapis.com/maps/api/place/autocomplete/json?input=${value}&types=(cities)&key=AIzaSyA5fYKOUOtx02o9l4TxzUiAvNslkhT8Nwc`,
        type = "GET",
        dataType = 'jsonp',
        cache = false;

  $.ajax({
      url, type, dataType, cache,
      success: function(response){
          alert(response);
      }
  });

In the second case, it also throws an error, but CORS. Made according to the doc: https://developers.google.com/api-client-library/j...
Inserted JS code:
<script src="https://apis.google.com/js/api.js" type="text/javascript"></script>
<script type="text/javascript">
  gapi.load('auth2', init);
</script>

Writes typeError to init. Tell me how to connect correctly? Created a project in the console, received the key

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2017-06-06
@Div100

Is your init initialized somewhere? If so, why pass undefined there?

function init() {
        gapi.client.init({
          'apiKey': '...',
          'discoveryDocs': [...],
          ...
        }).then(...)
      };

      function loadClient() {
        gapi.load('auth2', init);
      }

And about the first option, I suppose it falls on what jsonp comes in, it tries to parse it as json.
Look at the answer, there should be a function

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question