如何在任何地方使用Cors反向代理和添加CORS标头 [英] How to use Cors anywhere to reverse proxy and add CORS headers

查看:164
本文介绍了如何在任何地方使用Cors反向代理和添加CORS标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了两个小时的反向代理文档,以添加CORS标头,但我无法使用。

I've been reading for two hours the documentation of this Reverse proxy to add CORS headers, and I'm not able to use. Can you please help with a simple example how to use that.

CORS-ANYWHERE

我已经在javascript中尝试过该示例

I've tried that example in a javascript

(function() {
var cors_api_host = 'cors-anywhere.herokuapp.com';
var cors_api_url = 'https://' + cors_api_host + '/';
var slice = [].slice;
var origin = window.location.protocol + '//' + window.location.host;
var open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
    var args = slice.call(arguments);
    var targetOrigin = /^https?:\/\/([^\/]+)/i.exec(args[1]);
    if (targetOrigin && targetOrigin[0].toLowerCase() !== origin &&
        targetOrigin[1] !== cors_api_host) {
        args[1] = cors_api_url + args[1];
    }
    return open.apply(this, args);
};
})();

我真的不明白我是否需要node.js还是需要什么

I don't understand really if I need node.js or what exactly

推荐答案

CORS Anywhere可帮助从其他网站访问数据,而这些数据通常被Web浏览器的相同来源策略所禁止。这是通过服务器(在这种情况下,用Node.js编写)将对这些站点的请求代理完成的。

CORS Anywhere helps with accessing data from other websites that is normally forbidden by the same origin policy of web browsers. This is done by proxying requests to these sites via a server (written in Node.js, in this case).

要使用API​​,只需在URL前面加上API URL。 。真的就是全部。因此,您将请求 https://cors-anywhere.herokuapp.com/http:/,而不是请求 http://example.com /example.com 。然后,CORS Anywhere将代表您的应用程序发出请求,并将CORS标头添加到响应中,以便您的Web应用程序可以处理响应。

"To use the API, just prefix the URL with the API URL.". That's really all of it. So, instead of requesting http://example.com, you will request https://cors-anywhere.herokuapp.com/http://example.com. CORS Anywhere will then make the request on behalf of your application, and add CORS headers to the response so that your web application can process the response.

问题的摘录如果需要,将自动修改 XMLHttpRequest 生成的请求的URL。此代码段不是必需的,您可以自己添加CORS Anywhere API URL,如在演示页面中

The snippet from your question automatically modifies the URL for requests generated by XMLHttpRequest if needed. This snippet is not required, you can just prepend the CORS Anywhere API URL yourself, as done in the demo page.

Github上的存储库( https://github.com/Rob--W/cors-anywhere )包含为CORS Anywhere提供动力的服务器的源代码。如果您是前端开发人员,那么这就是您所需要知道的。如果您的应用程序有很多用户,那么您应该自己托管CORS Anywhere,以避免耗尽公共CORS Anywhere服务器上的所有资源。

The repository on Github (https://github.com/Rob--W/cors-anywhere) contains the source code of the server that powers CORS Anywhere. If you are a front-end dev, then that's all you need to know. If your application has many users, then you should host CORS Anywhere yourself, to avoid eating up all resources on the public CORS Anywhere server.

这篇关于如何在任何地方使用Cors反向代理和添加CORS标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆