¿Cómo agregar índices a la matriz en la cadena de consulta?
Intenté enviar datos como este:
axios.get('/myController/myAction', { params: { storeIds: [1,2,3] })Y tengo esta url:
http://localhost/api/myController/myAction?storeIds[]=1&storeIds[]=2&storeIds[]=3Entonces, debería obtener esta url:
http://localhost/api/myController/myAction?storeIds[0]=1&storeIds[1]=2&storeIds[2]=3¿Qué debo agregar en mis opciones de parámetros para obtener esta URL?
Puede usar paramsSerializer y serializar parámetros con https://www.npmjs.com/package/qs
axios.get('/myController/myAction', { params: { storeIds: [1,2,3] }, paramsSerializer: params => { return qs.stringify(params) } })Sin tener que agregar más bibliotecas y usando ES6, podría escribir:
axios.get(`/myController/myAction?${[1,2,3].map((n, index) => `storeIds[${index}]=${n}`).join('&')}`);Muchas gracias por la respuesta de Nicu Criste , para mi caso, la API requiere parámetros como este:
params: { f: { key: 'abc', categories: ['a','b','c'] }, per_page: 10 } El método es GET y esta API requiere que el formato sea: API?f[key]=abc&f[categories][]=a&f[categories][]=b... Así que asigné el paramsSerializer de axios así:
config.paramsSerializer = p => { return qs.stringify(p, {arrayFormat: 'brackets'}) }qs por favor vaya a este enlace