将换行符字符(即\ r \n)附加到发布请求中发送的表单字段

Bri*_*汤莱恩 2 html javascript newline http-post textinput

我正在编写一些Web表单,用于测试学生编写的服务器作为练习.有些服务器不像规范工作那样完全符合规范.将Windows新行字符(即\r\n)附加到POST浏览器生成的请求会很有用,因为有些服务器正在执行ReadLine而不是使用Content-Length:标头计数.

我正在寻找生成以下请求(来自IE):

POST /Brian HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-GB
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost:43
Content-Length: 8
DNT: 1
Connection: Keep-Alive
Cache-Control: no-cache

location
Run Code Online (Sandbox Code Playgroud)

但是追加\r\n到最后.到目前为止,我这样做的尝试是:

<form> 
Username: <input type="text" name="url" id="url4"
    onChange="document.getElementById('update4').setAttribute('action', 
    'http://localhost:43/' + document.getElementById('url4').value);"> </input> 
</form>
<form id="update4" method="post" action="http://localhost:43/">
Location: <input type="text" name="isindex" id="location5" 
 onChange="document.getElementById('location5').value = 
             document.getElementById('location5').value + '\\\r\\\n'"> </input>
<input id="mysubmit3" type="submit" value="go" ></input>
</form>
Run Code Online (Sandbox Code Playgroud)

我知道在事情上附加新行可能很棘手,所以我正在寻找指导.我不希望将换行符编码为POST的一部分.我只是想\r\nPOST命令后发送一个原始文件来刷新帖子.

Sᴀᴍ*_*ᴇᴌᴀ 13

文本输入元素仅允许单行文本.根据W3C工作草案进行文本输入:

输入类型 =文本 - 文本输入字段

类型 ="文本"
指定其输入元件是输入元件的一个单行纯文本编辑控制.

value = 不带换行符的字符串
指定此input元素的值.
:任何不包含换行符(U + 000A,"LF")或回车符(U + 000D,"CR")字符的字符串.1

并根据属性type ='text' 的MDN文档<input>:

text:单行文本字段.换行符会自动从输入值中删除.2

因此,在文本输入元素的值中添加新行字符基本上是多余的.

替代方案可能包括:

  • 添加隐藏文本区域并设置文本区域的值以包含具有新行字符的文本字段的值,但可能难以防止学生取消隐藏文本区域并更改值.看到这个phpfiddle中演示的内容.

  • 使用FormData存储表单的值,添加新行字符,然后使用具有更改值的formdata提交表单.这可能只能通过异步提交表单(例如使用AJAX,提供给iframe等其他目标,其他窗口等)来实现.

可以使用几种不同的技术通过JavaScript添加新行,包括:

  • 换行符\n:

console.log("Bob\nis\ncool");
Run Code Online (Sandbox Code Playgroud)

console.log(`Bob 
is 
cool.`);
Run Code Online (Sandbox Code Playgroud)

console.log('line 1'+String.fromCharCode(10, 13)+'line 2')
Run Code Online (Sandbox Code Playgroud)


1 https://www.w3.org/TR/2012/WD-html-markup-20120315/input.text.html#input.text

2 https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input