【问题标题】:CORS Error on Google Places API but not Reddit [duplicate]Google Places API 上的 CORS 错误,但 Reddit 上没有 [重复]
【发布时间】:2019-11-05 08:09:20
【问题描述】:

我正在尝试进行 api 调用(Google Places API)。但是我不断收到以下错误

访问 XMLHttpRequest 在 'https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=Seoul&inputtype=textquery&fields=photos,formatted_address,name,rating,opening_hours,geometry&key=--snip--' 来自原点“http://localhost:8100”已被 CORS 策略阻止: 请求中不存在“Access-Control-Allow-Origin”标头 资源。

使用以下代码:

  getRemoteData() {
    this.http

      .get(
        "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=Seoul&inputtype=textquery&fields=photos,formatted_address,name,rating,opening_hours,geometry&key=--snip--"
      )
      .map(res => res)
      .subscribe(data => {
        console.log(data);
      });
  }

但是,如果我将网址替换为 https://www.reddit.com/r/gifs/top/.json?limit=105sort=hot

它工作正常。为什么呢?我该如何解决?

【问题讨论】:

    标签: javascript google-maps ionic-framework google-places-api ionic4


    【解决方案1】:

    这是另一个问题的重复: https://stackoverflow.com/a/42182716/1152102

    这是因为您使用了错误的 api,旨在供服务器使用。

    CORS 策略保护服务器用户,防止浏览器向服务器发送未经授权的请求以及用户凭据。

    【讨论】:

      猜你喜欢
      • 2017-09-12
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多