使用 angular 从 appveyor rest api 获取 CORS 错误

Getting CORS error from appveyor rest api using angular

我正在编写一个 angular 应用程序,该应用程序通过 $http 服务向 AppVeyor REST API to get my project last build status 发出 GET 请求,但出现 CORS 错误。

当我从 Fiddler 开始尝试时,它起作用了。

var buildStatus= 'https://ci.appveyor.com/api/projects/accountName/projectSlug';
var token = 'q3245dsfg'; // my account api token

$http.get(buildStatus, {
    headers: {
        'Authorization': 'Bearer ' + token
    }
 }).then(...

这是我在 Firefox 上遇到的错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://ci.appveyor.com/api/projects/accountName/projectSlug. (Reason: CORS request failed).

有什么想法吗?

由于浏览器安全 CORS 限制,您的请求失败。

例如,您的页面是从域 xyz.com 加载的。出于安全原因,浏览器仅允许来自域 xyz.com 的已加载页面的 ajax 请求。 如果您的页面向域 ci.appveyor.com 发送 ajax 请求,则外部域必须 return 'Access-Control-Allow-Origin' header 以您的站点域(或通配符)作为值。

据我所知,您无法更改远程服务器上的 CORS header,因此您还有其他功能 - 您可以使用服务器端代理:

1 - 您可以使用一些代理服务器(例如 nginx) 2 - 您可以从您的业务逻辑请求数据,您的逻辑可以从外部服务请求数据。

我正在从我的 Node.js 服务器向 AppVeyor 发出请求,并将响应转发回我的 angular 应用程序。