【问题标题】:How to GET variables in window.location.hash in node.js如何在 node.js 中的 window.location.hash 中获取变量
【发布时间】:2012-08-22 05:11:36
【问题描述】:

如果我有像

这样的网址
www.example.com/path#one=a1&two=a2&three=a3

如何直接从 node.js 中的 url 检索变量? 由于我使用的是 ExpressJS,因此我尝试使用 req.query.variable(和 req.params.variable),但这不起作用...

我的实际问题是通过使用客户端身份验证 (https://developers.facebook.com/docs/authentication/client-side/) 从 Facebook 获取访问令牌,但它重定向到的 url 是结构如下:

YOUR_REDIRECT_URI#
 access_token=USER_ACCESS_TOKEN
 &expires_in=NUMBER_OF_SECONDS_UNTIL_TOKEN_EXPIRES

而且我不知道如何从url中获取参数...

【问题讨论】:

  • 怎么样 url = require('url'); var url_parts = url.parse(urlString); console.log(url_parts);

标签: javascript facebook parsing node.js express


【解决方案1】:

如何直接从 node.js 中的 url 中检索变量?

完全没有,因为 URL 的哈希部分不会传输到服务器。

我的实际问题是通过使用客户端身份验证从 Facebook 获取访问令牌 [...] 而且我不知道如何从 url 获取参数...

您还必须“获取”它们客户端

【讨论】:

  • 哦哇...我怎么没有意识到...谢谢!我想我会尝试服务器端。
【解决方案2】:
function parseQueryString(query) {
    var parsed = {};

    query.replace(
        new RegExp('([^?=&]+)(=([^&]*))?', 'g'),
        function ($0, $1, $2, $3) {
            parsed[decodeURIComponent($1)] = decodeURIComponent($3);
        }
    );

    return parsed;
}

var url = "www.example.com/path#one=a1&two=a2&three=a3";

console.log(parseQueryString(url.split('#')[1]));

请注意,这不会尝试检测 url 参数的类型。

【讨论】:

    猜你喜欢
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2011-10-18
    相关资源
    最近更新 更多