Vue之Axios跨域问题解决方案

// axios 中的GET请求
axios.get('/user', {
    params: {
      ID: ‘001’
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

// axios 中的POST请求
axios.post('/user', {
    firstName: '1',
    lastName: '2'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

方案1:既然使用axios直接进行跨域访问不可行,咱们就须要配置代理了。代理能够解决的缘由:由于客户端请求服务端的数据是存在跨域问题的,而服务器和服务器之间能够相互请求数据,是没有跨域的概念(若是服务器没有设置禁止跨域的权限问题),也就是说,咱们能够配置一个代理的服务器能够请求另外一个服务器中的数据,而后把请求出来的数据返回到咱们的代理服务器中,代理服务器再返回数据给咱们的客户端,这样咱们就能够实现跨域访问数据。php

准备工做:安装所需中间件和插件等,好比axios,http-proxy-middleware等。css

具体案例:这里以访问豆瓣Top250为例,直接访问以下:前端

axios.get("http://api.douban.com/v2/movie/top250")
.then(res=>{
console.log(res)
})
.catch(err=>{
console.log(err)
})vue

当执行npm run dev时,控制台报错以下:webpack

Vue之Axios跨域问题解决方案

事实证实直接请求确实出现跨域问题了,下面具体演示解决跨域问题的步骤:ios

上面所说的必备条件都已安装完成的状况下,执行如下步骤便可解决问题:git

1.配置BaseUrlgithub

在main.js中,配置数据所在服务器的前缀(即固定部分),代码以下:web

// 项目入口,配置全局vue
import Vue from 'vue'
import VueRouter from './router/routes.js'
import Store from './store/index.js'

import './assets/less/index.less'
import App from './App.vue'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'

import axios from 'axios'
Vue.prototype.$axios = axios
axios.defaults.baseURL = '/api'  //关键代码
Vue.config.productionTip = false

Vue.use(ElementUI);

new Vue({
    router:VueRouter,
    store:Store,
    template:'<App/>',
    components: {App}
}).$mount('#app')

// 默认进入商品模块
// VueRouter.push({ path: '/home' })

关键代码:axios.defaults.baseURL = '/api',做用是咱们每次发送的请求都会带一个/api的前缀。npm

2.配置代理

在config文件夹下的index.js文件中的proxyTable字段中,做以下处理:

dev: {
    env: require('./dev.env'),
    port: 8090,
    autoOpenBrowser: true,
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
      '/api': {
        target:'http://api.douban.com/v2', // 你请求的第三方接口
        changeOrigin:true, // 在本地会建立一个虚拟服务端,而后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
        pathRewrite:{  // 路径重写,
          '^/api': ''  // 替换target中的请求地址,也就是说之后你在请求http://api.douban.com/v2/XXXXX这个地址的时候直接写成/api便可。
        }
      }
    },
    // CSS Sourcemaps off by default because relative paths are "buggy"
    // with this option, according to the CSS-Loader README
    // (https://github.com/webpack/css-loader#sourcemaps)
    // In our experience, they generally work as expected,
    // just be aware of this issue when enabling this option.
    cssSourceMap: false
  }

3.在具体使用axios的地方,修改url以下便可:

axios.get("/movie/top250").then((res) => {
                  res = res.data
                  if (res.errno === ERR_OK) {
                     this.themeList=res.data;
                  }
                }).catch((error) => {
                  console.warn(error)
                })

4.从新启动项目以后,已经解决了跨域问题,结果以下:
【注意】:必须重启项目!!

原理:

由于咱们给url加上了前缀/api,咱们访问/movie/top250就当于访问了:localhost:8080/api/movie/top250(其中localhost:8080是默认的IP和端口)。

在index.js中的proxyTable中拦截了/api,并把/api及其前面的全部替换成了target中的内容,所以实际访问Url是http://api.douban.com/v2/movie/top250。

至此,纯前端配置代理解决axios跨域获得解决。

根据评论区内容,区分一下生产环境和开发环境,集体配置以下:

1.在config文件夹里面建立一个api.config.js的配置文件

const isPro = Object.is(process.env.NODE_ENV, 'production')

console.log(isPro);

module.exports = {
baseUrl: isPro ? 'https://www.***/index.php/Official(线上地址)' : 'api/'
}
2.在main.js文件里面引入上面文件,这样就能够保证动态的匹配生产和开发环境的定义前缀了,代码以下:

import Vue from 'vue'
import App from './App'
import router from './router'
import 'bootstrap/dist/js/bootstrap.min'
import 'bootstrap/dist/css/bootstrap.min.css'
import axios from 'axios'
import apiConfig from '../config/api.config'

Vue.prototype.$axios = axios;
Vue.config.productionTip = false;
axios.defaults.baseURL = apiConfig.baseUrl;// 配置接口地址
axios.defaults.withCredentials = false;
以上两步便可解决vue的跨域问题,而且能够能够直接build打包到线上,若有问题,请评论区留言,但愿对你有所帮助。

点赞 20

相关文章
相关标签/搜索