How to correctly use axios params with arrays

后端 未结 7 493
臣服心动
臣服心动 2021-02-01 14:55

How to add indexes to array in query string?

I tried send data like this:

axios.get(\'/myController/myAction\', { params: { storeIds: [1,2,3] })
<         


        
7条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-01 14:59

    Thanks so much the answer from Nicu Criste, for my case, the API requires params like this:

    params: {
      f: {
        key: 'abc',
        categories: ['a','b','c']
       },
      per_page: 10
    }
    

    Method is GET and this API requires the format is: API?f[key]=abc&f[categories][]=a&f[categories][]=b... So I assigned the paramsSerializer of axios like this:

    config.paramsSerializer = p => {
          return qs.stringify(p, {arrayFormat: 'brackets'})
        }
    
    • Install qs please go to this link
    • Read more about paramsSerializer in axios document
    • Edit format of params: Read more at qs stringifying document

提交回复
热议问题