简体   繁体   English

如何从重定向的 url 获取查询数据

[英]How to get query data from redirected url

I have a card payment that returns a response with a redirect URL. I have to redirect the user after the payment has been initialized.我有一个卡支付返回一个重定向 URL 的响应。我必须在支付初始化后重定向用户。 I am doing that using:我这样做使用:

...
 window.location.href = response.data.meta.authorization.redirect

To have the payment validated on a page I do not have control over.要在我无法控制的页面上验证付款。

After validation I have a redirect URL in my request that redirects (browser redirect) the user back to my application with query data that looks like this:验证后,我的请求中有一个重定向 URL,它将用户重定向(浏览器重定向)到我的应用程序,查询数据如下所示:

http://account.localhost.com/deposit/card?response=%7B"id"%3A1499407,"txRef"%3A"tUhElMQhoC"...,

I need the id in that url to verify the payment and subsequently update my database.我需要 url 中的 ID 来验证付款并随后更新我的数据库。

How can I do this with Vue?我怎样才能用 Vue 做到这一点?

You can use URL API to retrieve the json in the response parameter, then parse it and get the id from resultant object.您可以使用URL API检索response参数中的 json,然后解析它并从结果 object 中获取 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)

With Vue-router, you can get query params with this.$route.query , then to access a specific parameter: this.$route.query.id使用 Vue-router,您可以使用this.$route.query获取查询参数,然后访问特定参数: this.$route.query.id

As the ID is inside a stringified JSON object, you'd have to parse it first: JSON.parse(this.$route.query.response).id由于 ID 在字符串化的 JSON object 中,您必须先解析它: JSON.parse(this.$route.query.response).id

So, with a redirect, it makes sense to access the data in the created lifecycle hook:因此,通过重定向,访问created的生命周期挂钩中的数据是有意义的:

created() {
    console.log(JSON.parse(this.$route.query.response).id);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM