Get request part after hash sign

北战南征 提交于 2019-12-30 08:07:16

问题


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

  1. User follows the link http://example.com/articles#/?tags=Mac%20OS
  2. During initial page rendering, all articles are fetched
  3. On the client side, hash-part is parsed and only "Mac OS"-related articles are requested via AJAX.
  4. 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:

  1. User follows the link http://example.com/articles#/?tags=Mac%20OS
  2. Server parses hash-part and returns "Mac OS"-related articles
  3. 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.


回答1:


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.




回答2:


@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.



来源:https://stackoverflow.com/questions/4275214/get-request-part-after-hash-sign

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!