Axios demo

chuangwang

ChuangWANG

Posted on May 2, 2021

Axios demo

Offical Axios website: https://zetcode.com/javascript/axios/
get_req.js

const axios = require('axios');
axios.get('http://localhost:3210/hehe/haha').then(resp => {
    console.log(resp.data);
}).catch((reject)=>console.log(reject));
Enter fullscreen mode Exit fullscreen mode

get_req_async.js

const axios = require('axios');
async function makeGetRequest() {
  let res = await axios.get('http://webcode.me');
  let data = res.data;
  console.log(data);
}
makeGetRequest();
Enter fullscreen mode Exit fullscreen mode

basic_api.js

const axios = require('axios');
async function makeRequest() {
    const config = {
        method: 'get',
        url: 'http://webcode.me'
    }
    let res = await axios(config)
    console.log(res.status);
}
makeRequest();
Enter fullscreen mode Exit fullscreen mode

head_req.js

const axios = require('axios');
async function makeHeadRequest() {
  let res = await axios.head('http://localhost:3210/hehe/haha');
  console.log(`Status: ${res.status}`)
  console.log(`Server: ${res.headers.server}`)
  console.log(`Date: ${res.headers.date}`)
}
makeHeadRequest();
Enter fullscreen mode Exit fullscreen mode

Http in webmanager frontend POST:

const { data } = await this.$http({
  url: '/common/resource/delete',
  method: 'post',
  data: idList
})
if (data.code === "200") {
  that.$message("删除成功")
} else {
  that.$message("删除失败")
}
Enter fullscreen mode Exit fullscreen mode

Http in webmanager frontend GET:

    async getUserInfo () {
      const { data } = await this.$http({
        url: this.$http.adornUrl('/sys/user/info'),
        method: 'get',
        params: this.$http.adornParams()
      })
      if (data && data.code === 200) {
        this.loading = false
        this.userId = data.user.userId
        this.userName = data.user.username
      }
    }
Enter fullscreen mode Exit fullscreen mode
const { data } = await this.$http({
            url: this.$http.adornUrl('/sys/user/password'),
            method: 'post',
            data: this.$http.adornData({
              password: this.dataForm.password,
              newPassword: this.dataForm.newPassword
            })
          })
          if (data && data.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$nextTick(() => {
                  this.mainTabs = []
                  clearLoginInfo()
                  this.$router.replace({ name: 'login' })
                })
              }
            })
          } else {
            this.$message.error(data.message)
          }
Enter fullscreen mode Exit fullscreen mode
async getFirstList (page) {
      const { data } = await this.$http(
        {
          url: '/content/page/list',
          type: 'get',
          params: {
            page: page,
            limit: 10
          }
        }
      )
      this.firstPannelPaginationSize = data.data.totalCount
      this.firstPannelPaginationNum = page
      this.firstPannelTableData = data.data.list
    },
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
chuangwang
ChuangWANG

Posted on May 2, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related