$http提供商向 Github API 发出外部请求,但不向 Google 地图 API 发出请求

$http provider makes external requests to Github API, but not Google map API

本文关键字:请求 API 地图 Google http 提供商 外部 Github      更新时间:2023-09-26

>我正在尝试使用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 在我的浏览器中都可以正常工作。

有人知道是什么导致谷歌地图API失败吗?

请求本身并没有完全失败。您遇到了经典的 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没有任何这样的标头。您可以在此处阅读更多相关信息