这几天在写一个项目,然后就遇到了请求的编码问题,然后在度娘上搜到了答案
// 请求拦截器配置处理
this.axiosInstance.interceptors.request.use((config: AxiosRequestConfig) => {
const {
headers: { ignoreCancelToken },
} = config;
let url = config.url
// get参数编码
if (config.method === 'get' && config.params) {
url += '?'
const keys = Object.keys(config.params)
for (const key of keys) {
url += `${key}=${encodeURIComponent(config.params[key])}&`
}
url = url.substring(0, url.length - 1)
config.params = {}
}
config.url = url
const ignoreCancel =
ignoreCancelToken !== undefined
? ignoreCancelToken
: this.options.requestOptions?.ignoreCancelToken;
!ignoreCancel && axiosCanceler.addPending(config);
if (requestInterceptors && isFunction(requestInterceptors)) {
config = requestInterceptors(config, this.options);
}
return config;
}, undefined);
我试了,是没有问题,记录一下,方便以后查阅