axios 跨域

main.js

import Axios from 'axios'

Vue.prototype.$axios = Axios;
Axios.defaults.baseURL = '/api';
Axios.defaults.headers.post['Content-Type'] = 'application/json';

 

vue2中vue.config.js无效,vue3中config/index.js不存在

vue2  config/index.js 的 module.exports 中

proxyTable: {
      '/api': {
        target: "http://127.0.0.1:8000",
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    },

vue3 package.json同级目录 新规 vue.config.js

module.exports = {
  //axios域代理,解决axios跨域问题
  devServer: {
    proxy: {
      '/api': {
        target: 'http://127.0.0.1:8000',
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          '/api':''
        }
      }
    }
  }
}

wk.vue 请求组件中 路径正常填写

 this.$axios.get('/login', {params: {ID: 12345}})
        .then(function (response) {
          console.log(response);
        })
        .catch(function (response) {
            console.log(response);
        });

 

 

* '/api' 按需求自定义

posted @ 2021-04-15 00:22  容我看看这世界啊  阅读(114)  评论(0)    收藏  举报