浏览器路由地址含有“#”,“?”之类的跳转就会报错404的解决办法

一般的前端页面跳转请求地址带参 比如 http://localhost:8777/#/crowd/customer/edit?id=256&crowdNo=DDA2023101009000001 直接加在后面即可,如果遇到复杂的比如对象为了不影响数据一般JSON.stringify(paramData)被跳转页面直接JSON.Parse(paramData)即可,但是遇到带有特殊符号的,比如“#”,“?”之类的浏览器跳转就会报错404,针对这种情况使用encodeURIComponent封装了2个函数。

/**
*参数处理 
*参数加密
*{"keywords":"圈选结果数据","blacklistCustomer":#123456} => keywords=%E5%9C%88%E9%80%89%E7%BB%93%E6%9E%9C%E6%95%B0%E6%8D%AE&blacklistCustomer=%23123456&
* @param {*} params  参数
*/
export function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    var part = encodeURIComponent(propName) + "=";
    if (value !== null && value !== "" && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']';
            var subPart = encodeURIComponent(params) + "=";
            result += subPart + encodeURIComponent(value[key]) + "&";
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&";
      }
    }
  }
  return result
}


/**
*参数处理 
*参数解密
 keywords=%E5%9C%88%E9%80%89%E7%BB%93%E6%9E%9C%E6%95%B0%E6%8D%AE&blacklistCustomer=%23123456&=>{"keywords":"圈选结果数据","blacklistCustomer":#123456} 
* @param {*} params  参数
*/
export function getQueryObject(url) {
  url = url == null ? window.location.href : url
  const search = url.substring(url.lastIndexOf('?') + 1)
  const obj = {}
  const reg = /([^?&=]+)=([^?&=]*)/g
  search.replace(reg, (rs, $1, $2) => {
    const name = decodeURIComponent($1)
    let val = decodeURIComponent($2)
    val = String(val)
    obj[name] = val
    return rs
  })
  return obj
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值