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

时间:2025-04-28 07:59:03

post请求

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

3. 通过FormData表单形式传递参数

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

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

get请求

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

2. 通过 params对象传递数据(推荐)
params参数必写 , 如果没有参数传{}也可以

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