新行字符的提交方式不同

发布于 2024-07-22 02:16:24 字数 386 浏览 5 评论 0原文

我想知道是否有人可以阐明这种浏览器行为:

我有一个带有文本区域的表单,该表单通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你是孤单的心事 2024-07-29 02:16:24

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.

枉心 2024-07-29 02:16:24

RFC2616(HTTP1.1) 的第 3.7.1 部分中,它允许 \r \n,\r,\n 代表换行符。

HTTP 放宽了这一要求并允许
传输仅用普通 CR 或 LF 表示一行的文本媒体
当整个实体主体一致完成时会中断。 HTTP协议
应用程序必须接受 CRLF、裸 CR 和裸 LF 作为
代表通过 HTTP 接收的文本媒体中的换行符。

但这不适用于控制结构:

这种灵活性涉及
换行符仅适用于实体主体中的文本媒体; 裸 CR
或 LF 不得在任何 HTTP 控制中替换 CRLF
结构(例如标头字段和多部分边界)。

In part 3.7.1 of RFC2616(HTTP1.1), it allows either \r\n,\r,\n to represent newline.

HTTP relaxes this requirement and allows the
transport of text media with plain CR or LF alone representing a line
break when it is done consistently for an entire entity-body. HTTP
applications MUST accept CRLF, bare CR, and bare LF as being
representative of a line break in text media received via HTTP.

But this does not apply to control structures:

This flexibility regarding
line breaks applies only to text media in the entity-body; a bare CR
or LF MUST NOT be substituted for CRLF within any of the HTTP control
structures (such as header fields and multipart boundaries).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文