1. 程式人生 > >vue使用axios實現非同步請求

vue使用axios實現非同步請求

首先,安裝axios和qs
然後,在main.js中引入

import axios from 'axios'
import qs from 'qs'

Vue.prototype.$axios =  axios
// @TODO 這裡qs怎麼全域性引入?我是前端小白。

在vue.config.js下,module.exports程式碼塊下寫以下程式碼:

  devServer: {
    proxy: {
      '/api': {
        target: 'http://127.0.0.1:8080/專案名',
        ws: true,
        changeOrigin: true,
        pathRewrite:{'^/api':''}
      }
    }
  }

axios使用:

let url = '/api/xxx.do'
var params = {
  type: 'edit',
  name: 'zs',
}
axios.post(url, qs.stringify(params)).then(res => {
  console.log(res)
}).catch(err => {
  console.log(err)
})

總結

  1. 如果不使用qs,後臺可能獲取不到資料
  2. 繼續更新