一、跨域是什么?
二、如何解决跨域问题?
跨域解决方案:
基本解决方案 JSONP只支持GET请求,JSONP优点是支持老式浏览器,不支持CORS网站请求数据。 不管是Node中间件代理还是nginx反向代理主要通过同源策略不限制服务器。 在日常工作中,使用较多的跨域方案是cors和nginx反向代理
主要解释CROS和Proxy两种方式
1、CROS
2、Proxy(代理)
1.可配置多种不同的配置proxy
devServer: {
proxy: {
'/api': {
//代理标志通常是每个接口前的相同部分 target: 'http://23.15.11.15:8000', // 这是访问界面的域名和端口号 changeOrigin: true, // 允许跨域请求 pathRewrite: {
// 重写路径,替换请求地址中的指定路径 '^/api': '/user' } }, '/login': {
target: 'http://23.15.11.15:8000', changeOrigin: true, pathRewrite:{
'^/login':'' ///替换空 } } } },
示例:
- http://localhost:8080/api/test --> http://23.15.11.15:8000/user/test
- http://localhost:8080/login/index–> http://23.15.11.15:8000/index
2.代理所有接口
devServer: {
proxy: 'http:/www.ljc.com' }
示例:
- http://localhost:8080/api/test --> http://www.ljc.com/api/test
- http://localhost:8080/login/index–> http:/www.ljc.com/login/index