javascript - 如何从重定向的 url 获取查询数据
问题描述
我有一张卡付款,它返回带有重定向 URL 的响应。付款初始化后,我必须重定向用户。我这样做是使用:
...
window.location.href = response.data.meta.authorization.redirect
要在我无法控制的页面上验证付款。
验证后,我的请求中有一个重定向 URL,它将用户重定向(浏览器重定向)回我的应用程序,查询数据如下所示:
http://account.localhost.com/deposit/card?response=%7B"id"%3A1499407,"txRef"%3A"tUhElMQhoC"...,
我需要该 url 中的 id 来验证付款并随后更新我的数据库。
我怎样才能用 Vue 做到这一点?
解决方案
您可以使用URL API检索参数中的 json response
,然后对其进行解析并从结果对象中获取 id。
const str = `http://account.localhost.com/depositcard?response=%7B"id"%3A1499407,"txRef"%3A"tUhElMQhoC"%7D`
const json = new URL(str).searchParams.get('response')
const obj = JSON.parse(json)
console.log(obj.id)
推荐阅读
- c# - Entityframework Core 3 linq 表达式无法翻译
- rundeck - 我可以在“作业”选项卡中关闭“作业活动”吗
- javascript - 单击取消按钮,折叠属性停止工作
- ruby - 为什么在迭代数组时会跳过 elsif 条件?
- prometheus - Prometheus 保留时间设置为 1 年,但不会将数据保留超过默认 15 天
- python - 为什么 PyGame 中的音频会出现此错误?
- python - 带有实例变量的 Pandas 自定义访问器
- python - 被阻止在 Python 中关闭文件
- python - Python - 从字符串中过滤数据 - 在逗号之前,中间和之后
- java - 相当于 Windows %PROGRAMDATA% 环境变量的 OSX/MacOS