在 vue 项目使用 axios 来发送get请求时,需要传递的参数是数组的时候,例如:
let myThings = ['1', '2', '3']
axios.get('/user', {
params: {
ID: 12345,
things: myThings ,
}
})
// 上面的请求是 '.../user?ID=12345&things[]=1&things[]=2&things[]=3
问题在于直接传的数组
axios.get('/user', {
params: {
ID: 12345,
things: myThings + '',
}
})
// 在参数后面加上( + ''),即可把数组变成字符串
// 请求变成 '.../user?ID=12345&things=1,2,3
JavaScript中将数组变成字符串:
let arr = [1,2,3]
console.log(arr + '')
// "1,2,3"
参考文章:https://blog.csdn.net/romeo12334/article/details/85096570