How do I pass the # symbol as part of a GET querystring in the URL?

試著忘記壹切 提交于 2019-12-10 12:18:50

问题


I am using javascript (using jquery) to pass a # symbol as a GET parameter via AJAX call.

The problem right now is that the # symbol is breaking up my querystring.

Any help appreciated. Thanks!


回答1:


You need to replace it with %23 in the string. However, instead of doing this directly you should use Javascript function encodeURIComponent to encode characters in the URL.

Alternatively, if you are using jQuery.ajax you can automatically encode parameters by passing them in via the data option.




回答2:


If you are passing the data parameter to jQuery.ajax, jQuery does the encoding for you. You shouldn't be trying to build the URL yourself.

$.ajax({ url : "http://myserver.com/mypage.aspx",
         data : {'key1' : 'value#', 'key2' : 'value&&'}
         ...
      });



回答3:


Use encodeURIComponent().




回答4:


You will have to encode the url using escape, encodeURIComponent function, etc to pass that character. This will turn the characters into their % counter-partts. For example, # will be %23



来源:https://stackoverflow.com/questions/2111751/how-do-i-pass-the-symbol-as-part-of-a-get-querystring-in-the-url

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