1 回答
![?](http://img1.sycdn.imooc.com/5458632800010f8802200220-100-100.jpg)
TA贡献1824条经验 获得超8个赞
似乎这是Axios 库的一个问题(或默认参数序列化行为)。
因此,要克服这一点,您有 2 个选择。
在 URL 本身中定义您的查询参数
const whitespace = "white space";
axios.get(`/api?space=${whitespace}`);
自己编写paramsSerializer以构建查询字符串。
const whitespace = "white space";
const encodeWhitespace = encodeURI(whitespace);
const noSpace = "no";
axios.get('/api', {
params: {
'foo': 'bar',
'bar': 'hello world',
'space': whitespace,
'simple': noSpace
},
paramsSerializer: (params) => {
// Sample implementation of query string building
let result = '';
Object.keys(params).forEach(key => {
result += `${key}=${encodeURIComponent(params[key])}&`;
});
return result.substr(0, result.length - 1);
}
});
注意:以上paramsSerializer也可以定义在全局级别或 Axios 实例级别。
全球层面
axios.defaults.paramsSerializer = (params) => { /* ... */ };
实例级别
let axInstance = axios.create({ paramsSerializer: (params) => { /* ... */ } })
添加回答
举报