Axios使用指南

使用Axios设置请求头(headers)

参考自:使用Axios来设置请求头(headers)的方法

get请求

axios.get(url, config)
  .then(res=> console.log(res))
  .catch(err=> console.log(err))

举例:

axios.get('/manger/getAllStudentInfo', {
      headers: {
        Authorization: `Bearer ${sessionStorage.getItem("token")}`
      }
    }).then(
      (response) => {
        if (response.data.code == 0) {
          let dataList = response.data.data.information
          dataList.map((item) => {
            return item.key = item.student_id
          })
          setDataSource(dataList);
        }
      }
    )

axios.post() 方法 设置请求头

axios.post('/your-url', data, {
  headers: {
    'Content-Type': 'application/json',
    Authorization: `Bearer ${sessionStorage.getItem("token")}`
    // other headers here...
  }
});

设置了 Content-Type 头,指定了请求的数据格式为 JSON

使用axios发送get、post请求 参数在query和body里面的各种情况

https://www.cnblogs.com/mdtayz/p/16858101.html

使用axios发送get请求,参数写在query里

let res = await this.$axios.get('/user/b/getOrgList', {
        params: {
            pageNo: 1,
            pageSize: 99,
            type: 6
        }
    })
    this.researchList = res.data.retData.list
},

使用axios发送post请求,参数写在body里

await this.$axios.post('/project/b/project/updateProjectOrg', {
        leader: this.leader,
        oldOrgId: this.orgPrimaryId,
        planSubjectNum: this.planSubjectNum,
        sectionId: this.mainResearchValue.sectionId,
        userId: this.mainResearchValue.userId,
        userName: this.mainResearchValue.name
    })
},

使用axios发送post请求,参数写在query里

async deleteRow(index, row) {
    await this.$axios.post(
        '/project/b/project/deleteProjectOrgById',
        {},
        {
            params: {
                orgPrimaryId: this.orgPrimaryId
            }
        }
    )
},

Parameter Type:query

axios.get('/manger/selectNumOfNoSampling', {
      params: {
        college: e || currentvalue
      },
      headers: {
        Authorization: `${sessionStorage.getItem("token")}`
      }
    }).then(
      (response) => {
        if (response.data.code === 0) {
          let res = response.data.data.number
          console.log(e, currentvalue, res);
          setRescount(res)
        }
      }
    )

Parameter Type:body

axios.post('/student/updateInfo', curStu, {
      headers: {
        Authorization: `${sessionStorage.getItem("token")}`
      }
    }).then(
      (res) => {
        console.log(res);
        if (res.data.code === 0) {
          getDataSource()
        }
        if (res.data.code === -1) {
          alert('修改失败')
        }
      }
    )

https://blog.csdn.net/qq_53810245/article/details/127007137

posted @ 2023-01-02 18:10  Lu西西  阅读(102)  评论(0)    收藏  举报