新行字符的提交方式不同
我想知道是否有人可以阐明这种浏览器行为:
我有一个带有文本区域的表单,该表单通过 XHR 提交到服务器(使用 jQuery,我也尝试过使用纯 XMLHttpRequest 只是为了排除 jQuery 和结果是相同的)或通过表单提交的“老式”方式。 在这两种情况下都使用 method="POST"。 两种方式都提交到服务器上的同一脚本。
现在有趣的部分是:如果您通过 XHR 提交,新行字符将被传输为“%0A”(或者 \n,如果我没记错的话),如果您以常规方式提交,它们将被传输为“%0D%0A”(或\r\n)。 当然,这会导致服务器端出现一些问题,但这不是这里的问题。 我只是想知道为什么会出现这种差异? 无论您使用哪种提交方法,新行不应该以相同的方式传输吗? 还有哪些其他差异(如果有)?
I was wondering if somebody could shed some light on this browser behaviour:
I have a form with a textarea that is submitted to to the server either via XHR (using jQuery, I've also tried with plain XMLHttpRequest just to rule jQuery out and the result is the same) or the "old fashioned" way via form submit. In both cases method="POST" is used.
Both ways submit to the same script on the server.
Now the funny part: if you submit via XHR new line characters are transferred as "%0A" (or \n if I am not mistaken), and if you submit the regular way they are transferred as "%0D%0A" (or \r\n).
This, off course, causes some problems on the server side, but that is not the question here.
I'd just like to know why this difference? Shouldn't new lines be transferred the same no matter what method of submitting you use? What other differences are there (if any)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XMLHttpRequest 将在发送 XML 时从流中去除 CR 字符。 这符合 XML 规范,该规范指示将 CRLF 规范化为简单 LF。
因此,如果您将内容打包为 XML 并通过 XHR 发送,您将丢失 CR。
XMLHttpRequest will when sending XML strip the CR characters from the stream. This is in accord with the XML specification which indicates that CRLF be normalised to simple LF.
Hence if you package your content as XML and send it via XHR you will lose the CRs.
在 RFC2616(HTTP1.1) 的第 3.7.1 部分中,它允许 \r \n,\r,\n 代表换行符。
但这不适用于控制结构:
In part 3.7.1 of RFC2616(HTTP1.1), it allows either \r\n,\r,\n to represent newline.
But this does not apply to control structures: