ant design+axios跨域实现

1.启动springboot后端,提供接口

在这里插入图片描述

2.在config文件夹下创建proxy.tx文件,如果存在,在dev中添加axios环境代理,例如在我的demo中添加了/asd映射到http://localhost:8889

/**
 * 在生产环境 代理是无法生效的,所以这里没有生产环境的配置
 * The agent cannot take effect in the production environment
 * so there is no configuration of the production environment
 * For details, please see
 * https://pro.ant.design/docs/deploy
 */
export default {
  
  dev: {
    '/api/': {
      target: 'https://preview.pro.ant.design',
      changeOrigin: true,
      pathRewrite: { '^': '' },
    },
    '/asd':{
      target:'http://localhost:8889',/*目标地址   */
      changeOrigin: true,           /* 可以跨域:true*/
    pathRewrite: { '^/asd': '' },/*将目标地址改为/asd   */
    }
  },
  test: {
    '/api/': {
      target: 'https://preview.pro.ant.design',
      changeOrigin: true,
      pathRewrite: { '^': '' },
    },
  },
  pre: {
    '/api/': {
      target: 'your pre url',
      changeOrigin: true,
      pathRewrite: { '^': '' },
    },
  },
  
};

3.在需要的地方使用

首先 import axios from 'axios'(前提是安装了axios)

function click1(){
  axios.get('/asd/test')
  .then(function(response) {
    console.log(LoginForm.UserName);
    console.log(LoginForm.Password);
    console.log(response.data);
    console.log(response.status);
    console.log(response.statusText);
    console.log(response.headers);
    console.log(response.config);
  });
}

4.npm start启动前端ant design pro,验证成功

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值