Javascript 如何在查询字符串中使用回车或换行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5065912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use carriage return or line feed in the query string?
提问by rsp
the customers can enter lines in a text area and this is saved in the database.
客户可以在文本区域中输入行,并将其保存在数据库中。
If the customer comes back to the site he can load the previously entered data.
如果客户回到站点,他可以加载之前输入的数据。
However, the line feeds and carriage returns aren't displayed in the text area.
但是,换行符和回车符不会显示在文本区域中。
I can place them in the query string for example by ASCII coding them: %A or %D but java doesn't like that and throws an IllegalArgumentException.
我可以将它们放在查询字符串中,例如通过 ASCII 编码它们:%A 或 %D 但 java 不喜欢这样并抛出 IllegalArgumentException。
So I do now: %5Cn and %5Cr which gives: \n and \r
所以我现在做: %5Cn 和 %5Cr 给出: \n 和 \r
How can I make javascript to display the escaped new lines as actual new lines in the text area?
如何让 javascript 将转义的新行显示为文本区域中的实际新行?
The URL is something like:
网址类似于:
http://www.abc.com?textarea=line1%5Cn%5Crline2
http://www.abc.com?textarea=line1%5Cn%5Crline2
and I want the line1 and line2 to be on two different lines in the textarea.
我希望 line1 和 line2 在 textarea 中的两条不同的行上。
回答by rsp
%5C
is a literal backslash - so %5Cn
means just "backslash and then the letter n". What you probably want is %0A
and %0D
instead of %A
and %D
. But you should URL-encode the entire string properly and not just encode two characters by hand. Use encodeURIComponent()
. Also, use POST
instead of GET
, but not because the string is multiline but because you are storing it in the database. You shouldn't use the GET
method for operations that are not idempotent.
%5C
是一个字面反斜杠 - 所以%5Cn
意味着只是“反斜杠然后是字母 n”。您可能想要的是%0A
and%0D
而不是%A
and %D
。但是您应该正确地对整个字符串进行 URL 编码,而不仅仅是手动编码两个字符。使用encodeURIComponent()
. 另外,使用POST
代替GET
, 但不是因为字符串是多行的,而是因为您将它存储在数据库中。您不应该将这种GET
方法用于非幂等的操作。
回答by Chris B
回答by adarshr
Using GET (query string) to enter multi-line data is wrong. You should be considering POST instead.
使用GET(查询字符串)输入多行数据是错误的。您应该考虑使用 POST。