AngularJS:如何清除 URL 中的查询参数? [英] AngularJS: How to clear query parameters in the URL?

查看:30
本文介绍了AngularJS:如何清除 URL 中的查询参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 AngularJS 应用程序需要访问用户的 LinkedIn 个人资料.为此,我需要将用户重定向到包含回调 redirect_uri 参数的 LinkedIn URL,该参数将告诉 LinkedIn 将用户重定向回我的 web 应用程序并在 URL 中包含代码"查询参数.这是传统的 Oauth 2.0 流程.

My AngularJS application needs to have access to the user's LinkedIn profile. In order to do that I need to redirect the user to a LinkedIn URL which contains a callback redirect_uri parameter which will tell LinkedIn to redirect the user back to my webapp and include a "code" query param in the URL. It's a traditional Oauth 2.0 flow.

除了 LinkedIn 将用户重定向回以下 URL 之外,一切都很好:

Everything works great except that LinkedIn redirects the user back to the following URL:

http://localhost:8080/?code=XXX&state=YYY#/users/123/providers/LinkedIn/social-sites

我想从 URL 中删除 ?code=XXX&state=YYY 以使其干净.用户不需要查看我从 LinkedIn 重定向收到的查询参数.

I would like to remove ?code=XXX&state=YYY from the URL in order to make it clean. The user does not need to see the query parameters I received from LinkedIn redirect.

我尝试了 $location.absUrl($location.path() + $location.hash()).replace(),但它在 URL 中保留了查询参数.

I tried $location.absUrl($location.path() + $location.hash()).replace(), but it keep the query params in the URL.

我也无法提取查询参数,例如代码",使用 ($location.search()).code.好像有?上面 URL 中的 # 之前是在欺骗 Angular.

I am also unable to extract the query parameters, e.g. "code", using ($location.search()).code. It seems like having ? before # in the URL above is tricking Angular.

推荐答案

我最终从 AngularJS 论坛得到了答案.请参阅此主题了解详情

I ended up getting the answer from AngularJS forum. See this thread for details

该链接指向 Google 网上论坛帖子,该帖子难以阅读且未提供明确答案.要删除 URL 参数,请使用

The link is to a Google Groups thread, which is difficult to read and doesn't provide a clear answer. To remove URL parameters use

$location.url($location.path());

这篇关于AngularJS:如何清除 URL 中的查询参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆