1. 程式人生 > 其它 >vue 跨域處理

vue 跨域處理

技術標籤:nginxvue

兩種辦法
1.在vue.config.js進行跨域

    proxy: {
      // 配置跨域
      '/api': {
      //我伺服器的地址
        target: 'http://localhost:10000',
        // ws:true,
        changOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    }

前端傳送請求
在這裡插入圖片描述

然後這個請求會被代理到 http://localhost:10000/user/login
2.使用nginx

nginx配置檔案

server {
        listen       12210;
        server_name  localhost;
		add_header Access-Control-Allow-Origin *;
		add_header Access-Control-Allow-Headers X-Requested-With;
		add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
        #charset koi8-r;

        #access_log  logs/host.access.
log main; location / { root html; index index.html index.htm; } location /api { rewrite ^.+api/?(.*)$ /$1 break; include uwsgi_params; proxy_pass http://localhost:10000; }

前端請求埠換成nginx的埠12210
http://localhost:12210/api/user/login

會被代理到http://localhost:10000/user/login