Vue项目调后台.Net core接口localhost:8080与localhost:5001跨域

在vue项目内的vue.config.js文件中

module.exports内找devServer,如果没有手动添加。

代码:

module.exports = {
  devServer: {
    host:"localhost",
    port:8080,
    proxy: {
      "/api": { //自定义
        "target": "https://localhost:5001", //这里可以跟随项目实际部署服务器来
        ws: true,
        secure: false,// 这个跨https时一定要加
        changeOrigin: true,
        pathRewrite: {
          "^/api": "/api"//自定义
        }
      }
    }  
  }
}

我的后台服务启动:https://localhost:5001/api/user

前台启动后:http://localhost:8080/#/myDemo

在我的myDemo页面import了axios

<script>
import axios from 'axios'
export default {
  name: 'myDemo',
  data(){
    return {
      myTest: null
    }
  },
  methods: {
  getMyTest() {
      axios.get('/api/user')
      .then((res) => {
        this.myTest = res.data.datas;
        console.log(res)
      })
      .catch((err) => {
        console.log(err)
      })
    }
 },
  mounted() {
    this.getMyTest();
  }
}
</script>

axios想使用需要敲命令引到项目中,在项目根目录下敲以下

npm install --save axios

鸣谢文章:vue——前端跨域

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值