zoukankan      html  css  js  c++  java
  • Vue3 axios跨域配置

    安装axios

    npm install axios vue-axios

    main.js里

    import axios from 'axios'
    import VueAxios from 'vue-axios'
     
    const app = createApp(App) // 创建实例
     
    app.config.globalProperties.$axios = axios
    axios.defaults.baseURL = '/aposServer'
    axios.defaults.headers.post['Content-Type'] = 'application/json';
    
    app.config.productionTip = false
    app.use(VueAxios,axios);

    在package.json同级目录下加个vue.config.js文件

    module.exports = {
      devServer: {
        proxy: {
          '/aposServer': {
              target: 'https://localhost:44310/api', //API服务器的地址
              changeOrigin: true,
              pathRewrite: {
                  '/aposServer': ''
              }
          }
      },
      }
    }

    调用webapi

    this.$axios.post("/Equipments/GetAllEquipmentsTest")
    .then(res=>{
       console.log(res)
    })
    .catch(err=>{
       console.log(err)
    })
                }

    重启下服务

  • 相关阅读:
    【leetcode】图像渲染
    【leetcode】不邻接植花
    052-75
    052-74
    052-73
    052-71
    052-70
    052-69
    052-67
    052-66
  • 原文地址:https://www.cnblogs.com/czly/p/14480485.html
Copyright © 2011-2022 走看看