【问题标题】:$http provider makes external requests to Github API, but not Google map API$http 提供者向 Github API 发出外部请求,但不向 Google 地图 API 发出请求
【发布时间】:2014-12-05 16:51:45
【问题描述】:

我正在尝试使用 AngularJS 的 $http 提供程序来创建对 Google Map API 的请求。我不断收到错误消息:

use 'strict';

if (!headersObj) headersObj =  parseHeaders(headers);

  if (name) {
    return headersObj[lowercase(name)] || null;
  }

  return headersObj;
}

我在这里创建了一个示例: http://jsfiddle.net/omniphx/dkagwpu0/16/

对 Github API 的请求会成功,但对 Google 地图 API 的请求会失败。无法弄清楚我可能会错过什么。两者似乎都输出 JSON 格式,并且两个 URL 在我的浏览器中都可以正常工作。

有人知道导致 Google Maps API 失败的原因吗?

【问题讨论】:

  • “失败”是个大词——它是如何失败的?
  • HTTP Get 请求失败。它在我创建的小提琴中重现。
  • 什么是状态码(请记住回答问题所需的一切都需要在问题中,而不是在 jsfiddle 上)
  • 感谢您的帮助。状态码 = 0
  • 地图 API URL 未返回 CORS 标头

标签: javascript angularjs google-maps httprequest


【解决方案1】:

请求本身并不完全失败。 您遇到了经典的 CORS 问题。

在控制台中找到XMLHttpRequest cannot load http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.

Github 的标头中有 Access-Control-Allow-Origin:* 标头,而 googleapis 没有任何此类标头。你可以阅读更多关于它here

【讨论】:

猜你喜欢
  • 2022-10-06
  • 2014-04-16
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-07
  • 2021-07-10
相关资源
最近更新 更多