vue-cli 配置 跨域 axios

在 config 文件夹 找到 index.js

## 找到  proxyTable:{}

改成 :

    proxyTable: {
      '/api': {
        target: 'http://xxxxxx.com/', // 请求地址
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    },

使用 '/api' 代替你的 地址

例 : 你的 地址 是 'http://xxxxxx.com/xxx/xxxx/xxxxx' ,

就可以写成 '/api/xxx/xxxx/xxxxx'

配置 完成 之后 重启 npm run dev


axios文档 https://www.kancloud.cn/yunye/axios/234845

npm install axios https://github.com/axios/axios

CDN https://unpkg.com/axios/dist/axios.min.js

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

import axios from 'axios'
const baseUrl = '/api';
axios.defaults.baseURL = `${baseUrl}*******<请求地址 />`;

// headers 带 token
axios.interceptors.request.use(response => {
  // Do something with response data
  response.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
  response.headers['token'] = 'token <token />';
  console.log(response);
  return response;
}, error => {
  // Do something with response error
  console.log(error);
  return Promise.reject(error);
});

axios.post('/地址', {
       name: '小红',
       age: '18'
  })
  .then(function (res) {
    console.log(res);
  })
  .catch(function (error) {
    console.log(error);
  });

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,568评论 2 45
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    wgl0419阅读 6,359评论 1 9
  • 1.配置用户名和邮箱: git config --global user.name "名字(英文)" git co...
    佩佩216阅读 1,124评论 0 0
  • 1 Webpack 1.1 概念简介 1.1.1 WebPack是什么 1、一个打包工具 2、一个模块加载工具 3...
    Kevin_Junbaozi阅读 6,733评论 0 16
  • 心很沉下去,去找自己去了,自己究竟该做怎样的人?不想和别人联系,不想再从别人那里得到评价,你口中的我不是我,不想再...
    快刀b阅读 222评论 0 0