首页 > 解决方案 > 如何从重定向的 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 做到这一点?

标签: javascriptvue.js

解决方案


您可以使用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)


推荐阅读