JW Player (api.jwplatform.com) - CORS问题

在使用 Angular2 开发的 Web 应用程序中,我在使用 JW Platform 服务时遇到了麻烦。

我应该假设 JWplayer 在 'api.jwplatform.com' 终点不正确地管理 CORS 请求吗?

以下是情况。我发送了以下请求

Request URL:https://api.jwplatform.com/v1/channels/list/?api_format=xml&api_key=BNgTpOY5&api_nonce=33502681&api_timestamp=1487003685&types_filter=manual&api_signature=287df9d7dcefaf9bcae656c518a4cc29718bcaeb
Request Method:GET
Status Code:200 OK
Remote Address:10.68.96.40:8080

这些是请求标头:

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4,fr;q=0.2,de;q=0.2,es;q=0.2
Connection:keep-alive
Host:api.jwplatform.com
Origin:https://mydomain
Referer:https://mydomain/channels
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36

其中 mydomain 当然是我发送请求的域名。

下面是我从终点收到的响应标头:

Cache-Control:no-cache
Connection:keep-alive
Content-Length:3847
Content-Type:application/xml; charset=utf-8
Date:Mon, 13 Feb 2017 16:34:46 GMT
Pragma:no-cache
Server:openresty
X-RateLimit-Limit:60
X-RateLimit-Remaining:58
X-RateLimit-Reset:1487003700

如上所示,响应中没有 'Access-Control-Allow-Origin: *' 标头,因此应用程序不被允许访问检索到的数据。

请注意,在浏览器控制台中,我既可以看到 _状态码: 200 OK_,也可以看到服务器返回的 _JSON 数据_。 所有这一切都在代理后面工作,但这只能在开发环境中正常工作。

谢谢您的帮助。

点赞
用户441757
用户441757

我是否应该假定JWplayer在“api.jwplatform.com”端点上无法正确处理CORS请求?

是的。

因此,如果您想让它正常工作,唯一实用的解决方案是设置反向代理或使用开放的反向代理,例如https://cors-anywhere.herokuapp.com/

您需要将请求直接发送到https://api.jwplatform.com/v1/channels/list/?...,而是发送到https://cors-anywhere.herokuapp.com/https://api.jwplatform.com/v1/channels/list/?...,然后进行代理请求,并向浏览器回复Access-Control-Allow-Origin头和其他所需CORS头。

您可以使用cors-anywhere包在Node.js中设置自己的CORS Anywhere实例:

npm install --save cors-anywhere
2017-02-14 00:09:31