url地址参数进行编码和解码

希望在 URL 中传递的参数值在编码后不可见,但在使用时能够解码为原始值,可以使用一种加密的方法(例如 Base64 编码)

在前端发送参数时进行 Base64 编码:  btoa

// 确保 access_token 存在
const accessToken = 'url 地址'

if (accessToken) {
  // 对参数进行 Base64 编码
  const encodedUsername = btoa(username)  // Base64 编码
  const encodedUserRole = btoa(userRole)  // Base64 编码
  const encodedUserlabel = encodeURIComponent(userlabel)  // URL 编码

  // 构造包含查询参数的 URL
  const url = `${accessToken}?username=${encodedUsername}&userrole=${encodedUserRole}&userlabel=${encodedUserlabel}`

  // 在新窗口中打开构造的 URL
  window.open(url)
} else {
  console.error('Access token is missing')
}

接收方再进行 Base64 解码:atob

// 从 URL 中获取参数值
const urlParams = new URLSearchParams(window.location.search)
const encodedUsername = urlParams.get('username')
const encodedUserRole = urlParams.get('userrole')
const encodedUserlabel = urlParams.get('userlabel')

// 对参数进行 Base64 解码
const username = atob(encodedUsername)  // Base64 解码
const userRole = atob(encodedUserRole)  // URL 解码
const userlabel = decodeURIComponent(encodedUserlabel)  // URL 解码

console.log(`Username: ${username}, UserRole: ${userRole}, UserLabel: ${userlabel}`)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值