VUE 解决跨域问题 Access-Control-Allow-Origin-试错

时间:2024-03-19 20:50:46

首先,这不是后端服务器 nginx的问题,如下设置不起作用:

    server {
        listen       6000;
        server_name  localhost;

        location / {
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization, X-Requested-With';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            return 204;
        }

        if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' '*'; # 或者指定具体的源,例如:http://localhost:8080
            add_header 'Access-Control-Allow-Credentials' 'true'; # 如果需要携带cookie进行跨域请求,请添加此行
        }


            proxy_pass http://127.0.0.1:6000;  # 将请求代理到不同的端口上
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

第二,这不是VUE的问题,百度了很多VUE文件设置和npm install --save ****