问题描述:从url上取参数然后发送给后端,使用decodeURICompnent空格会转为+
![](https://img-blog.csdnimg.cn/img_convert/37ab598f460fb7b2e75a7c271ccbd156.jpeg)
如上图所示,query中的引号中应该是空格,现在传给后端的是+
处理:
// decodeURIComponent(query[1]?.replace(/\+/g, '%20')) 把加号全局替换为%20,就可以解决上述问题
export const getUrlArgsWithDecode = () => {
const obj = {}
const url = window.location.search.substr(1)
if (url) {
url.split('&').forEach((query: any) => {
query = query.split('=')
obj[query[0]] = decodeURIComponent(query[1]?.replace(/\+/g, '%20'))
})
}
return obj
}
最后传给后端的数据如下:
![](https://img-blog.csdnimg.cn/img_convert/86e18440cc0afe32c4e0159154e9b624.jpeg)