博客
关于我
vue实现本地解决跨域 nginx实现部署解决跨域
阅读量:609 次
发布时间:2019-03-12

本文共 2678 字,大约阅读时间需要 8 分钟。

这里就不解释什么是跨域了。

跨域是前端最头疼的事情,它阻止了我们向后端请求数据,使之我们无法拿到数据去渲染。

当然,后端可以解决跨域,而且相当简单。但是如果再后端没办法的情况下,前端如果处理这种问题呢?

一:开发环境下(就是vue项目本地开发,没打包部署前)

1、vue cli2.x (npm run dev运行的项目):

我们可以在config文件夹中找到webpack的配置文件,其中的 index.js 文件中可以找到对应的跨域的配置,详情如下

module.exports = {  dev: {    // Paths    assetsSubDirectory: 'static',    assetsPublicPath: '/',    proxyTable: {        '/web1': {            target: 'http://192.1.2.33:5000/api/StudentSourceManage',// 这里是你实际真正请求的地址            changeOrigin: true,            pathRewrite: {                '^/web1': ''// 同上            }        },    },  },}

这里用web1(名字可自定义)代替实际的请求地址(写接口的公共部分即可),然后在调用接口的地方用web1代替此地址

2、vue cli3.x(npm run serve运行的项目):

vue cli3中对于webpack的配置文件都是不可见的,但是我们可以自写配置去覆盖它

在项目的根路径下新建个vue.config.js文件,然后在文件中写上跨域的配置即可

module.exports = {  devServer: {    proxy: {      '/web': {        target: 'http://192.1.2.3:9000', // 对应自己的接口        changeOrigin: true,        ws: false,        pathRewrite: {          '^/web': '',        },      },    },  },};

二、nginx部署vue项目实现解决跨域:

虽然上述配置解决了我们开发环境的跨域问题,但是vue基于打包会删除对应配置,那么打包后上述的配置就不会再起作用,那么我们如何解决呢?可以部署nginx解决此问题

看下nginx的配置文件:

#user  nobody;worker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    server {        listen       8080;        server_name  10.8.9.94;        location ^~ /api {            proxy_pass   http://192.1.2.3:9000;            add_header Access-Control-Allow-Methods *;            add_header Access-Control-Max-Age 3600;            add_header Access-Control-Allow-Credentials true;            add_header Access-Control-Allow-Origin $http_origin;            add_header Access-Control-Allow-Headers $http_access_control_request_headers;            if ($request_method = OPTIONS ) {                return 200;            }        }        location / {            root   xiangmu/dist;            index  index.html index.htm;            add_header 'Access-Control-Allow-Origin' '*';            try_files $uri $uri/ /index.html;        }        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }}

上述配置文件中,http对象中包括一个server,是用来搭建服务器的,linsten和server_name是你项目要部署的地址和端口号,location后面的/api是为了快速匹配下面的对应地址,proxy_pass是实际要请求的服务器地址,第二个location下面的root指向项目地址,这里是xiangmu文件夹下的dist文件夹,因为我们知道vue项目打包后会生成dist文件夹,文件夹中会有一个index.html文件

配置好文件后,我们把dist文件放到对应位置,然后启动nginx应用程序即可。(需要nginx的配置文件,请联系博主

然后我们再浏览器中访问我们的部署地址即可。(文中的部署地址是10.8.9.94:8080)

好了,以上就解决了我们前端跨域的问题,开发环境和正式部署环境都能解决跨域。

如有问题,请指出,接收批评。

个人微信公众号:

转载地址:http://smwaz.baihongyu.com/

你可能感兴趣的文章
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>
MySQL DBA 进阶知识详解
查看>>
Mura CMS processAsyncObject SQL注入漏洞复现(CVE-2024-32640)
查看>>