首页 > 移动平台 > 详细

axios 跨域

时间:2021-04-15 09:26:39      阅读:35      评论:0      收藏:0      [点我收藏+]

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‘ 按需求自定义

axios 跨域

原文:https://www.cnblogs.com/sqcokb/p/14660671.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!