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圖片示例
相關文章
相關標籤/搜索