axios发送get、post请求的几种方式

post请求

  1. 采用字符串拼接的方式进行传参
this.axios.post(`https://..../updateAddress?addressName=${this.addressName}&houseNumber=${this.houseNumber}&userName=${this.userName}&userPhone=${this.userPhone}&isdefault=${this.isDefault}&addressId=${this.addressId}`)
            .then(response => {
                //...
            })
复制代码
  1. json对象提交数据
let data = {
        'user': { 'id': `${this.$user.getUserId()}` },
        'hideFlag': '1',
        'content': this.content,
        'pictureUrl': this.imgUrl
      }
      this.axios.post('https://...../submitFeedback', data)
        .then(this.postFeedBackSucc)
        .catch((e) => {
          console.log(e)
        })
复制代码

postman图片示例
3. 经过FormData表单形式传递参数

  • 示例一:json对象字符串传递数据
    (我也不知道本身在说什么,应该是后端想要一个json对象来存储要传递的数据,可是这个json对象又是被转为字符串类型的)
let data = new FormData()
      data.append('json', JSON.stringify({ 'userId': `45435465464` }))
      this.axios.post('https://houqin.eooker.com/delivery/a/shop/merchantAPI/markList', data, {
        header: {
          'Content-Type': 'multipart/form-data'
        }
      })
        .then(response => {
            //.....
        })
        .catch((e) => {
          console.log(e)
        })
复制代码

postman图片示例

  • 示例二:经过键值对的表单形式传递数据
var formData=new FormData();
formData.append('user',123456);
formData.append('pass',12345678);
 
axios.post("/notice",formData)
     .then((res) => {return res})
     .catch((err) => {return err})
复制代码

postman图片示例

get请求

  1. 字符串拼接方式(不推荐)
_self.axios.get(`https://...../pay?orderNumber=${_self.orderNumber}&openId=${_self.$user.getOpenId()}`).then(result => {
        //.....
      }).catch(err => {
        console.log(err)
      })
复制代码

postman图片示例
2. 经过 params对象传递数据(推荐)
params参数必写 , 若是没有参数传{}也能够

let data = { userId: this.$user.getUserId(), start: 0, size: 10 }
    this.axios.get('https://..../listByUserId', { params: { 'json': data } })
    .then(response => {
        //.....
    })
    .catch((e) => {
        console.log(e)
    })
复制代码

postman图片示例
相关文章
相关标签/搜索