简体   繁体   English

在浏览器中从 URL 获取数据

[英]Get data from URL in a browser

The title might be a little misleading, so I'll try to explain it the best way I can.标题可能有点误导,所以我会尽力解释它。 In my Android application I'm using OAuth authentication.在我的 Android 应用程序中,我正在使用 OAuth 身份验证。 At one point, I need to open up a browser window so the user can approve the application.有一次,我需要打开浏览器 window 以便用户批准申请。 Once they approve it, they get forwarded to another URL with some address parameters.一旦他们批准它,他们就会被转发到另一个带有一些地址参数的 URL。 What I need is to grab the value from one of the parameters and feed it back into my application.我需要的是从其中一个参数中获取值并将其反馈到我的应用程序中。

What I'm kind of looking for is described in steps three and four in this Android & OAuth tutorial , I'm just not exactly sure if I should go the way described in the article.我正在寻找的内容在此 Android 和 OAuth 教程的第三和第四步中进行了描述,我只是不确定是否应该按照文章中描述的方式 go。

Thanks for all the help!感谢所有的帮助!

To get URL parameters via JavaScript in the browser, try something like this:要在浏览器中通过 JavaScript 获取 URL 参数,请尝试以下操作:

function getURLParameters() {
  var keyValuePairs = location.search.split(/[&?]/g);
  var params = {};
  for (var i = 0, n = keyValuePairs.length; i < n; ++i) {
    var keyValuePair = keyValuePairs[i];
    var eqIndex = keyValuePair.indexOf("=");
    if (eqIndex >= 0) {
      params[decodeURIComponent(keyValuePair.substring(0, eqIndex))]
        = decodeURIComponent(keyValuePair.substring(eqIndex + 1));
    }
  }
  return params;
}

If the same URL parameter (by name) is specified twice, then it will only show up once in the output, but you could change this to return a multimap like object.如果相同的 URL 参数(按名称)指定了两次,那么它只会在 output 中显示一次,但您可以更改它以返回像 ZA8CFDE6331BD59EB2AC96F8911C4B666 这样的多映射

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

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