简体   繁体   English

在POST上,ENTER从\\ n转换为\\ r \\ n

[英]ENTER converted from \n to \r\n on POST

I have this simple test page saved as page1.html. 我将这个简单的测试页保存为page1.html。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <script type="text/javascript">
      function submitForm() {
        alert(escape(document.myform.mytextarea.value));
        return true;
      }
    </script>
  </head>

  <body>
    <form name="myform" action="page2.html" method="post" onsubmit="javascript:return submitForm();">
      <textarea name="mytextarea">xxxyyy</textarea>

      <input type="submit" value="submitForm">
    </form>
  </body>
</html>

And this page saved as page2.html. 此页面保存为page2.html。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <body>
    Page2.html
  </body>
</html>

I open page1.html under Firefox (I think version has no importance, but it is 18.0. And same problem with Chrome or IE 10.0). 我在Firefox下打开page1.html(我认为版本没有重要性,但它是18.0。与Chrome或IE 10.0相同的问题)。 Before I click the submitForm button, I hit one ENTER between values "xxx" and "yyy" like this. 在我单击submitForm按钮之前,我在值“xxx”和“yyy”之间按下了一个ENTER。

HTML表单

When I click the submitForm button, the alert shows one character between "xxx" and "yyy" which is the \\n encoded as "%0A". 当我单击submitForm按钮时,警报显示“xxx”和“yyy”之间的一个字符,即\\ n编码为“%0A”。

Javascript警报

If I have a look into Firebug of what is posted, I can see two characters which are "\\r\\n" encoded as "%0D%0A". 如果我查看发布的Firebug,我可以看到两个字符“\\ r \\ n”编码为“%0D%0A”。

Firebug POST

Can you explain why \\n is transformed to \\r\\n on POST and how to prevent that ? 你能解释为什么\\ n在POST上转换为\\ r \\ n以及如何防止这种情况? I minimized my problem, but it's really problematic for me. 我最小化了我的问题,但这对我来说真的很麻烦。

I could test this under Safari on MAC OS, and I also get %0D%0A on POST. 我可以在Safari OS上测试这个,我也在POST上获得%0D%0A。

Under IE 8.0 and before, Javascript alerts %0D%0A and I get %0D%0A on POST, so IE 8.0 and before does not behave the same. 在IE 8.0之前和之前,Javascript警告%0D%0A并且我在POST上获得%0D%0A,因此IE 8.0及之前的行为不同。

New line characters are treated differently for different Operating Systems. 对于不同的操作系统,新行字符的处理方式不同。

\\r = CR (Carriage Return) // Used as a new line character in Unix \\ r = CR(回车)//用作Unix中的新行字符

\\n = LF (Line Feed) // Used as a new line character in Mac OS \\ n = LF(换行)//在Mac OS中用作换行符

\\r\\n = CR + LF // Used as a new line character in Windows \\ r \\ n = CR + LF //在Windows中用作换行符

As far as the data in text-area is concerned. 就文本区域的数据而言。 When the form is submitted the text-area contents are url-encoded and as per the HTML Spec : 提交表单时,文本区域内容是按网址编码的,并且符合HTML规范

Line breaks, as in multi-line text field values, are represented as CR LF pairs, ie `%0D%0A'. 与多行文本字段值一样,换行符表示为CR LF对,即“%0D%0A”。

I found a relevant SO Question that addresses a similar issue with line breaks and cross-platform compatibility. 我找到了一个相关的SO问题 ,解决了与换行和跨平台兼容性类似的问题。

As far as your problem is concerned if you want new lines to be treated uniformly, you could do a simple regex replace in the contents of your text area to take care of the discrepancy. 就您的问题而言,如果您想要统一处理新行,您可以在文本区域的内容中进行简单的正则表达式替换以处理差异。

Thanks to your informations, I wrote this function to count the "submitted" length of my textarea on any browser : 感谢您的信息,我写了这个函数来计算我的textarea在任何浏览器上的“提交”长度:

function getFormURLEncodedLength(myValue) {
  return myValue.replace(/(\r\n|\n|\r)/g, '\r\n').length;
}

Solution 1: 解决方案1:

in c#: 在c#中:

string value = Request.Params["txtValue"].replace("\r\n","\n");

Solution 2: 解决方案2:

Or you can add hidden field like: 或者您可以添加隐藏字段,如:

<input id="txtValue" type="textarea" />
<input id="hiddenTxtValue" type="hidden"/>

and set his value with encodeUriComponent : 并使用encodeUriComponent设置其值:

$('#hiddenTxtValue').val(encodeUriComponent($('#txtValue').val()));

in c#: 在c#中:

string value = Request.Params["hiddenTxtValue"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM