获取哈希符号后的请求部分
我的网站有 AJAX 支持的搜索,使用深度链接。当用户点击链接时……
http://example.com/articles#/?tags=Mac%20OS,review
标签“Mac OS”和“评论”应该已经在搜索表单中选择,并且与“Mac OS”和“评论”相关的文章应该出现在页面上。
我有以下情况,需要修复
- 用户点击链接 http://example.com /articles#/?tags=Mac%20OS
- 在初始页面渲染期间,所有文章都会在客户端获取
- ,哈希部分会被解析,并且仅通过 AJAX 请求与“Mac OS”相关的文章。
- 客户端接收“Mac OS”文章并替换在步骤 2 中获取的所有文章。还将“Mac OS”标签标记为在搜索表单上选择。
这里的问题是重复的文章渲染,这对用户来说看起来非常糟糕。他查看了所有文章,几秒钟后,它们将被替换为“Mac OS”文章。
我需要有以下场景:
- 用户点击链接 http://example.com/articles# /?tags=Mac%20OS
- 服务器解析哈希部分并返回“Mac OS”相关文章
- 客户端了解“Mac OS”文章已经存在并且不执行任何操作。它只是将“Mac OS”标签标记为已选择。
为此,我需要获取请求字符串的哈希部分:
/?tags=Mac%20OS
我无法在 ? 之后使用请求参数,因为我使用 AJAX 和深度链接。使用 ?-part,浏览器将被迫重新加载页面。我需要做任何事情而不重新加载页面。
我们将不胜感激您的帮助。
谢谢。
My web-site has AJAX-powered search, that uses deep-linking. When user follows a link …
http://example.com/articles#/?tags=Mac%20OS,review
… tags "Mac OS" and "review" should already been selected in a search form and articles, related to "Mac OS" and "review" should be presented on the page.
I have following scenario, that a need to fix
- User follows the link http://example.com/articles#/?tags=Mac%20OS
- During initial page rendering, all articles are fetched
- On the client side, hash-part is parsed and only "Mac OS"-related articles are requested via AJAX.
- Client receives "Mac OS"-articles and replaces all articles, fetched at step 2. Also it marks "Mac OS" tag as selected on a search-form.
The problem here - is duplicated articles rendering, that looks really bad for the user. He looks at all articles, and after couple of seconds, they will be replaced with "Mac OS"-articles.
I need to have following scenario:
- User follows the link http://example.com/articles#/?tags=Mac%20OS
- Server parses hash-part and returns "Mac OS"-related articles
- Client understands, that "Mac OS"-articles are already there and does nothing. It just marks "Mac OS" tag as selected.
To do this, i need to get hash-part of the request string:
/?tags=Mac%20OS
I cannot use request parameters after ?, because i use AJAX and deep-linking. With ?-part, browser will be forced to reload the page. I need to do anything without reloading the page.
You help will be greatly appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哈希值之后的 URL 部分不会发送到服务器,因此您无法在那里对其进行处理。您可以在创建 Ajax 请求的客户端代码中提取 URL 的部分并将其作为参数发送。
The part of a URL after the hash is not sent to the server, so you can't process it there. You can extract that part of the URL in the client-side code that creates your Ajax request and send it as a parameter.
@NickFitz 是正确的,但如果您必须发送
#
哈希/磅符号之后的任何内容,则可以使用代表#
的 URL 编码字符,即% 23
。因此,
%23
以及%23
之后的任何内容都将发送到服务器。如果您使用现代网络服务器,它们会自动识别%23
是#
。在 Ruby on Rails 中,Rack 会为您完成此任务。@NickFitz is correct, but if you must send whatever comes after the
#
hash/pound symbol, you can use the URL encoded characters that represent#
which is%23
.So
%23
and whatever that comes after%23
will be sent to the server. If you are using modern web server, they will automatically recognize that%23
is#
. In Ruby on Rails, Rack does this for you.