AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 977579
Accepted
HackXIt
HackXIt
Asked: 2019-08-02 05:08:29 +0800 CST2019-08-02 05:08:29 +0800 CST 2019-08-02 05:08:29 +0800 CST

VueJS 向 Flask 发送 POST 失败(CORS 请求被阻止)

  • 772

我一直在关注这个vuejs 指南和这个烧瓶指南,将我的前端和后端托管在树莓派上。

在我的前端,我有这个方法,它将 axios POST 发送到后端。

// path = http://127.0.0.1:5000/shift
// pin, port = 1-8 / SER1

sendByte(pin, port) {
  console.debug(`Setting ${pin} on ${port}`);
  // I'm adding the header to the payload directly
  const payload = {
    data: {
      pin,
      port
    },
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Content-Type": "application/json"
    }
  };
  console.debug(payload);
  axios.post(this.paths.shift, payload);
}

但是我的后端没有收到有效负载(因为 uwsgi.log 中没有任何有效负载),而是在控制台中收到此错误:

11:53:38.551 new-submission event fired Setup.vue:52
11:53:38.578 Watch-Handler for submissions fired (localStorage updated) Setup.vue:33
11:53:42.312 Setting 1 on SER1 Visualization.vue:83
11:53:42.313
Object { pin: 1, port: "SER1" }
Visualization.vue:85
11:53:45.151 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5000/shift. (Reason: CORS request did not succeed). 2
11:53:45.154 Error: Network Error createError.js:16
11:53:46.351 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5000/togglePort. (Reason: CORS request did not succeed). 2
11:53:46.353 Error: Network Error createError.js:16

因为它与此错误最相关,所以这是我的 nginx.conf:

server {
    listen      80;
    server_name fire.com;
    charset utf-8;
    root    /var/www/fire-extinguish-visualizer/dist;
    index   index.html index.htm;    # Always serve index.html for any request
    location / {
        try_files $uri /index.html @fireFlask;
    }
    location /static {
        root /var/www/fire-extinguish-visualizer/dist/;
    }
    location @fireFlask {
        include uwsgi_params;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' '*';
        # uwsgi_pass unix:/var/www/fire-extinguish-visualizer/server/uwsgi.sock;
        # uwsgi_pass 127.0.0.1:5000;
        uwsgi_pass uwsgi://localhost:5000;
    }
    error_log  /var/log/nginx/vue-app-error.log;
    access_log /var/log/nginx/vue-app-access.log;
}

我已经尝试了许多配置和设置,但我无法让它工作。

在所有这些尝试之后,我不想发布我尝试过的每个 nginx.conf 或 uwsgi.ini,但是为了更好地衡量,我的相关文件在这个 gist中。

我的问题是:

如何在 SENDER 和 RECEIVER 端正确设置 CORS 以避免此错误?

据我了解,完成以下操作后它应该可以工作:

  • Nginx 将 CORS 标头添加到来自托管应用程序的 POST 请求
  • uWSGI 配置正确
  • Flask 应用程序已安装 CORS 并允许跨域请求

那里还有什么?我现在只是对这个跨域错误感到困惑。

在 uwsgi.conf 中使用 http 时,我可以使用 curl 来获得正确的响应:

pi@firepi:~ $ curl -X POST http://localhost:5000/togglePort -d '{"port":"SER1", "trigger":0}' -H 'Content-Type:
 application/json'
{"status":"success"}
pi@firepi:~ $ curl -X POST http://localhost:5000/shift -d '{"port":"SER1", "pin":1}' -H 'Content-Type: application/json'
{"status":"success"}

尝试带有标题和来源的卷曲给出了这个:

pi@firepi:~ $ curl --include -X OPTIONS http://localhost:5000/togglePort -d '{"port":"SER1","trigger":0}' --header Access-Control-Request-Method:POST --header Access-Control-Request-Headers:Content-Type --header Origin:http://localhost:80
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Allow: OPTIONS, POST
Access-Control-Allow-Origin: http://localhost:80
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT
Vary: Origin
Content-Length: 0

我也偶然发现了uwsgi-tools,但我真的不知道如何使用 uwsgi_curl 发送特定的 CORS 标头。不过,这将有助于解决此问题,因为我可以缩小范围。有什么想法吗?

nginx
  • 1 1 个回答
  • 2623 Views

1 个回答

  • Voted
  1. Best Answer
    HackXIt
    2019-08-09T07:28:27+08:002019-08-09T07:28:27+08:00

    由于我的代码/配置中的 3 个错误配置而发生此问题。

    更正 #1

    正如@Slytherin 在评论中建议的那样,为了使 Nginx 正常工作,我需要将 POST 发送到正确的端口。在我的 Vue-App 中,我已经编程将 POST 直接发送到 Flask-App,这会导致 CORS 错误,因为 Nginx 无法添加适当的标头。我通过修复路径来修复它:

    ...
    paths: {
            // This is a static Address. I later changed it to a hostname: firepi:80
            togglePort: "http://192.168.137.139:80/togglePort",
            shift: "http://192.168.137.139:80/shift"
          }
    ...
    

    需要注意的是,当localhost作为地址使用时,浏览器实际上会指向它自己。因此,如果您在服务器 X 上运行您的应用程序并从 PC A 访问它,则 localhost 的路径实际上将在 PC A 中解析,而不是在服务器 X 中。因此,建议使用正确设置的主机名。

    更正 #2

    因为我的主要错误发生在问题 1(Nginx 端口)中,所以稳定地查看问题 2 是非常错误的。(CORS 配置)要启用 CORS,我需要在Nginx 配置中添加正确的标头,还需要配置我的 Flask-App 中的 CORS。对于 Flask,我只使用了默认值CORS(app)。

    在我的特殊情况下,我允许所有来源,因为此应用程序位于本地网络内,但在将应用程序连接到互联网时,您应该明确限制此类访问。

    更正 #3

    由于我将应用程序定向到端口 80 上的地址,因此我需要在 Nginx 中配置该链接:

    location /togglePort {
            include uwsgi_params;
            uwsgi_pass unix:/var/www/fire-extinguish-visualizer/server/app_uwsgi.sock;
    }
    location /shift {
            include uwsgi_params;
            uwsgi_pass unix:/var/www/fire-extinguish-visualizer/server/app_uwsgi.sock;
    }
    
    • 0

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve