我用axios创建一个get请求:
this.$axios.get('/cidade/',{
params: {
q: result,
}
})
.then((response) => {
this.cidades = response.data;
})
.catch(function (error) {
// handle error
// eslint-disable-next-line
console.log(error);
})
.then(function () {
// always executed
});
但是我的结果是一个数组[123asdas1,asdasd2312],当axios执行请求时,他创建了该URL:
http://localhost:8081/cidade/?q[]=5b9cfd0170607a2e968b0a1e&q[]=5b9cfd0170607a2e968b0a1d
因此可以从q参数中删除[]吗?怎么样?
tks
答案 0 :(得分:1)
在其中一个字段具有多个值(即,如果它是一个数组)的情况下组成查询字符串时,没有标准说明必须如何在查询字符串中对其进行编码,但是大多数Web服务器都接受以下语法:>
http://localhost:8081/cidade/?q[]=value1&q[]=value2
这就是axios默认使用它的原因。检查您的Web服务器,看看它是否正在正确读取参数作为数组。
如果要强制以其他方式进行编码,只需将数组转换为所需格式的字符串,然后将其作为单个值发送即可:
this.$axios.get('/cidade/', {
params: {
q: JSON.stringify(result)
}
})
http://localhost:8081/cidade/?q=[value1,value2]
([
和]
字符可能是percent-encoded。)
通常,此语法无法区分字符串"[value1,value2]"
和数组[value1, value2]
,因此Web服务器将不得不选择一个。同样,这都取决于您的Web服务器。