将url和查询参数转换为一个查询参数:

时间:2022-10-21 11:24:03

I have an url:

我有一个网址:

var url = /company/overview

I have query params

我有查询参数

var params = {
    campaign = "back-to-work"
    user = "1"
}

How can I convert these two values (originally extracted from $location.path() and $location.search()) into one query parameter called return_to. I will later attach this return_to query parameter to a login url.

如何将这两个值(最初从$ location.path()和$ location.search()中提取)转换为一个名为return_to的查询参数。我稍后会将此return_to查询参数附加到登录URL。

Github do something similar when trying to access content that requires login:

尝试访问需要登录的内容时,Github会做类似的事情:

https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Forgs%2FCompany%2Fdashboard%3Fcampaign%3Dback-to-work

1 个解决方案

#1


0  

You can use

您可以使用

encodeURIComponent($location.path())

which returns the following for this page

返回此页面的以下内容

"%2Fquestions%2F39034879%2Fconvert-url-and-query-parameters-into-one-query-parameter"

You just add this as a param to you other url and you're good to go :)

你只需将它作为一个参数添加到你的其他网址,你很高兴去:)

Additional Info:
There are two encoding options in JavaScript:

附加信息:JavaScript中有两种编码选项:

  • encodeURI() will not encode: ~!@#$&*()=:/,;?+'

    encodeURI()不会编码:〜!@#$&*()=:/ ,;?+'

  • encodeURIComponent() will not encode: ~!*()'

    encodeURIComponent()不会编码:〜!*()'

See Best practice: escape, or encodeURI / encodeURIComponent for more details.

有关详细信息,请参阅最佳实践:escape或encodeURI / encodeURIComponent。

#1


0  

You can use

您可以使用

encodeURIComponent($location.path())

which returns the following for this page

返回此页面的以下内容

"%2Fquestions%2F39034879%2Fconvert-url-and-query-parameters-into-one-query-parameter"

You just add this as a param to you other url and you're good to go :)

你只需将它作为一个参数添加到你的其他网址,你很高兴去:)

Additional Info:
There are two encoding options in JavaScript:

附加信息:JavaScript中有两种编码选项:

  • encodeURI() will not encode: ~!@#$&*()=:/,;?+'

    encodeURI()不会编码:〜!@#$&*()=:/ ,;?+'

  • encodeURIComponent() will not encode: ~!*()'

    encodeURIComponent()不会编码:〜!*()'

See Best practice: escape, or encodeURI / encodeURIComponent for more details.

有关详细信息,请参阅最佳实践:escape或encodeURI / encodeURIComponent。