Google Places API - 未捕获的SyntaxError:意外的令牌:

时间:2017-05-06 21:18:57

标签: javascript jquery json jsonp google-places-api

我已经研究了一个多小时了,无法找到有效的解决方案。

我的js:

 var googleUrl = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=fr&key=<MY KEY>'

  $.ajax({
      type: 'GET',
      url: googleUrl,
      dataType: 'jsonp',
      success: function(response)    {

          console.log(response)
       },
      error: function(response)    {
          console.log(response);
      }

  })

我看到了this question,它提出了一种回调方法&#39; - 我仍然无法弄清楚为什么我会收到错误:Uncaught SyntaxError: Unexpected token :

我发现它已被标记为重复 - 但正如我已经指出的那样,我已尝试过其他帖子中的所有内容,而且我仍然无法使用JSONP来获取工作。如何解决问题的建设性反馈将是有用的。

任何建议都表示赞赏!!

更新 - 显然我的代码中的密钥不是&#39;我的密钥&#39;。

1 个答案:

答案 0 :(得分:1)

您的语法错误是由于googleUrl使用您指定为dataType参数的JSON而非JSONP的响应。

因此,只需将dataTypejsonp更改为json

您可以详细了解此格式here