【问题标题】:Is there a way for a client side app to work with server, which doesn't have CORS set有没有办法让客户端应用程序与没有设置 CORS 的服务器一起工作
【发布时间】:2019-10-16 01:00:59
【问题描述】:

我想调用在用户系统中运行的第 3 方桌面应用程序,该应用程序的端口在 3000 处打开,当用户打开我的网站时,我向他的 localhost:3000 端口发出请求并获取数据,但是第 3 方应用程序没有为 CORS 设置响应标头,因此我无法使用此策略进行同步,有人知道这样做的方法吗? 在这里,我使用 ngrok 来隧道,但仍然无法做到。

xhr.open("POST", 'https://576b4cc1.ngrok.io', true);
//Send the proper header information along with the request
xhr.setRequestHeader("Content-Type", "text/plain");
xhr.onreadystatechange = function () { // Call a function when the state changes.
    if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
        // Request finished. Do processing here.
        console.log("In ready state exchange: " + xhr.responseText);
        resolve();
    }
}
xhr.onload = function (data) {
    console.log("In onload : " + data);
}
xhr.onerror = function (err) {
    console.log("In error: " + JSON.stringify(err, ["message", "arguments", "type", "name"]));
    console.log('Data incase : ' + xhr.statusText);
}
xhr.send(reqObj.data);

【问题讨论】:

  • CORS 要求是一种安全措施。没有变通方法。
  • 在理想情况下,您将设置一个代理来处理正确的 CORS 标头,并让它将请求中继到第 3 方应用程序。如果第 3 方应用程序不打算通过添加 CORS 标头来使其应用程序更安全。

标签: javascript browser cors xmlhttprequest


【解决方案1】:

您可以使用禁用网络安全的标志从命令行启动浏览器,例如使用 chrome,您可以执行以下操作:

chrome --disable-web-security

还有一些 chrome 插件可以让你像 https://chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf 那样做这个

【讨论】:

  • 我可以补充一点,这是非常危险的,如果您希望其他人使用和依赖您的应用程序,则永远不应该使用它。更安全的方法是设置一个提供正确 CORS 标头的代理服务器,然后再与外部 3rd 方应用程序通信。
猜你喜欢
  • 2010-12-23
  • 2010-11-04
  • 2021-02-07
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 2010-09-12
  • 1970-01-01
相关资源
最近更新 更多