1.安装axios

npm i --save axios

2.main.js 引入axios

import axios from 'axios'
Vue.prototype.axios = axios

3.在项目methods/使用

Get

this.axios({
          method:'get',
          url:'test.php',
          params:{state:"join"},
        }).then((response) =>{
           console.log(response['data'])//成功数据
        }).catch((error) =>{
          console.log(error)//错误数据
          alert(error)
        })
    },
    

Post

      this.axios({
        method:'post',
        url:'test.php',
        data:data,
      }).then((response) =>{
        console.log(response);//成功数据

      }).catch((error) =>{
        console.log(error)//错误数据
      })
    },

第一次用axios坑

axios 默认post 是用request payload来传输的

let data = new FormData()
data.append('data1','data1')
this.axios({
        method:'post',
        url:'lottery.php',
        data:data,
      }).then((response) =>{
        console.log(response);//成功数据

      }).catch((error) =>{
        console.log(error)//错误数据
      })
    },


本文固定链接: http://www.js-code.com/vue-js/vue-js_25021.html