axios.get使用陣列發送 params with arrays


使用params可不用全部欄位都擠在同一行編寫
後續維護編輯相對較好查找

type1. 欄位[0]=陣列[0]

axios.get('/myController/myAction', {
  params: { storeIds: [1,2,3] },
  paramsSerializer: {
    indexes: true, // use brackets with indexes
  }
)

結果: /myController/myAction?storeIds[0]=1&storeIds[1]=2&storeIds[2]=3

type2. 欄位=陣列[0]

axios.get('/myController/myAction', {
  params: { storeIds: [1,2,3] },
  paramsSerializer: {
    indexes: null, // no brackets at all
  }
)

結果: /myController/myAction?storeIds=1&storeIds=2&storeIds=3

type3. 欄位[]=陣列[0]

axios.get('/myController/myAction', {
  params: { storeIds: [1,2,3] },
  paramsSerializer: {
    indexes: false, // brackets but no indexes
  }
)

結果: /myController/myAction?storeIds[]=1&storeIds[]=2&storeIds[]=3



參考來源:How to correctly use axios params with arrays

關於站主

Shiro

因為興趣無限擴張,一直很猶豫要不要寫一個很雜的Blog,後來還是這麼做了。

聯絡:shiro050102✦gmail.com  ✦請自行更換成@

Vueaxios