【问题标题】:How can I decode part of the query string of a URL in my AngularJS application?如何在 AngularJS 应用程序中解码部分 URL 的查询字符串?
【发布时间】:2015-01-16 12:50:29
【问题描述】:

我有这个代码:

var search = $location.search();
    if (angular.isDefined(search.load) && search.load != null) {
        if (search.load = "confirmEmail")
            authService.confirmEmailUserId = search.userId;
            authService.confirmEmailCode = search.code;
            $state.transitionTo("auth.content", {
                content: search.load
            });
    }

它在 app.run 中,它查看用于打开应用程序的 URL。

但是 search.code 是用 C# 编码的

 var callbackUrl  = "http://localhost:2757/index.html" +
                    "?load=email" +
                    "&userId=" + user.Id +
                    "&code=" + HttpUtility.UrlEncode(code);

如何取回代码在传递给 UrlEncode 之前的原始值?

【问题讨论】:

  • 使用 JavaScript:decodeURIComponent().
  • @AndreiV - 你能把这个作为答案,所以我可以接受。

标签: javascript c# angularjs


【解决方案1】:

您可以使用decodeURIComponent() JavaScript 函数。此函数将 URL 编码字符串作为参数并返回其未编码形式。

在您的情况下,您可以解码整个 URL

var search = decodeURIComponent($location.search());

或者只是代码部分

authService.confirmEmailCode = decodeURIComponent(search.code);

【讨论】:

    猜你喜欢
    • 2016-03-22
    • 1970-01-01
    • 2016-05-10
    • 2017-06-29
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    相关资源
    最近更新 更多