如何在数组中正确使用 axios 参数 [英] How to correctly use axios params with arrays

查看:37
本文介绍了如何在数组中正确使用 axios 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在查询字符串中为数组添加索引?

How to add indexes to array in query string?

我尝试像这样发送数据:

I tried send data like this:

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

我得到了这个网址:

http://localhost/api/myController/myAction?storeIds[]=1&storeIds[]=2&storeIds[]=3

所以,我应该得到这个网址:

So, I should to get this url:

http://localhost/api/myController/myAction?storeIds[0]=1&storeIds[1]=2&storeIds[2]=3

我应该在我的 params 选项中添加什么来获取这个 url?

What I should add in my params options to get this url?

推荐答案

您可以使用 paramsSerializer 并使用 https://www.npmjs.com/package/qs

You can use paramsSerializer and serialize parameters with https://www.npmjs.com/package/qs

axios.get('/myController/myAction', {
  params: {
    storeIds: [1,2,3]
  },
  paramsSerializer: params => {
    return qs.stringify(params)
  }
})

这篇关于如何在数组中正确使用 axios 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆