Vue.js跨域请求代理与axios传参

1.config/index.js

module.exports = {
  dev: {

  }
}

2.在dev花括号添加配置

proxyTable: {
      '/api':{
        target:'http://localhost:8080/kzy',//跨域域名路径
        changeOrigin:true,//允许跨域
        pathRewrite:{
          '^/api':''//代替target中地址(例如要访问的接口test,访问路径可写成/api/test)
        }
      }
    }

3.请求

   import axios from 'axios';
   import qs from 'qs';//转JSON插件

   methods: {
      goTo () {
        axios.post('/api/findByCategory', qs.stringify({name:"小明"}))
          .then(response => {
            console.log(response);
          })
          .catch(err => {
            console.log(err);
          });
        }
     }
 

安利:Vue官方推荐组件axios默认就是提交 JSON 字符串。

猜你喜欢

转载自blog.csdn.net/One_small_yard/article/details/84973955