1 回答

TA貢獻1824條經(jīng)驗 獲得超8個贊
似乎這是Axios 庫的一個問題(或默認參數(shù)序列化行為)。
因此,要克服這一點,您有 2 個選擇。
在 URL 本身中定義您的查詢參數(shù)
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) => { /* ... */ } })
添加回答
舉報