Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

axios 的 get 请求,URL 参数存在空格变成加号(+) 的问题 #88

Open
cklwblove opened this issue Dec 13, 2021 · 0 comments
Open

Comments

@cklwblove
Copy link
Owner

cklwblove commented Dec 13, 2021

问题分析可以参考:axios/axios#1111

解决方案:

import qs from 'qs';

axios.defaults.paramsSerializer: function(params) {
  return qs.stringify(params, { indices: false }); // param=value1&param=value2
}

或者

service.interceptors.request.use(config => {
let url = config.url
    if (config.method === 'get' && config.params) {
        url += '?' // 拼接参数
        // 获取所有参数,通过循环 拼接所有参数,encodeURIComponent对参数编码,
        Object.keys(config.params).map(key => {
            url += `${key}=${encodeURIComponent(config.params[key])}&`
        })
        url = url.substring(0, url.length - 1) // 删除最后一个&字符
        config.params = {} // 参数已经存在于 url中
    }
    config.url = url
    return config
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant