在客户端启用CORS [英] Enabling CORS on the client side

查看:73
本文介绍了在客户端启用CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个在任何情况下都无法管理或修改的服务器上运行的网络应用程序。

I'm creating a web appliacation that will run on a server that I can not manage nor modify in any case.

在该应用程序中,我需要执行对其他服务器的AJAX调用。
这将始终被相同原产地政策阻止。

Within that application, I need to exceute a AJAX call to a different server. This will always be blocked by the 'Same Origin Policy'.

其中server01.test.net是Web服务器,而mail.test.net是第二台服务器。

Where server01.test.net is the webserver and mail.test.net is the second server.

有没有一种方法可以通过客户端的任何方式启用CORS,因为我无法在服务器上添加 Access-Control-Allow-Origin *。还是其他任何工作方式?

Is there a way to enable CORS by any means in the client side, as I'm not able to add the 'Access-Control-Allow-Origin "*"' on the server. Or any other workaournd?

谢谢

推荐答案

如果服务器是正在呼叫不支持CORS,您将无法使用AJAX向第三方服务器发出请求。

If the server you are calling does not support CORS, you will not be able to make the request to the third-party server using AJAX.

您将不得不求助于设置通行证通过您的应用程序中的AJAX路由。客户端(浏览器)向您的AJAX路由发出请求,该路由将调用代理到第三方服务器并返回结果。由于第三方请求是在服务器上而不是在浏览器上发生的,因此不适用相同来源策略。

You will have to resort to setting up a pass-through AJAX route in your application. The client (browser) makes a request to your AJAX route which proxies the call to the third-party server and returns the result. Because the third-party request is happening on the server rather than the browser, Same Origin Policy doesn't apply.

这种方法意味着会有一个附加请求如果可以使用CORS,则没有必要,但实际上没有其他选择。

This approach means there will be an additional request that wouldn't be necessary if you could use CORS, but there really isn't another option.

这篇关于在客户端启用CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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