问题描述:从url上取参数然后发送给后端,使用decodeURICompnent空格会转为+
如上图所示,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
}
最后传给后端的数据如下:
好文阅读
发表评论