参数有中包含空格且使用Post提交时须将空格转换成加号
jQuery的serialize模块中有个r20正则
1 | var r20 = /%20/g, |
jQuery.param方法中会将所有的"%20"转成"+",即提交数据前,数据中如果包含空格,那经过encodeURIComponent后,空格会转成"%20"
1 | encodeURIComponent( ' ' ) === '%20' ; // true |
最后需要将"%20"转换成"="再Post提交。这样后台程序接受到的才是真正的空格。
关于 encodeURIComponent,见MDC描述
encodeURIComponent
escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( )
To avoid unexpected requests to the server, you should call encodeURIComponent
on any user-entered parameters that will be passed as part of a URI. For example, a user could type "Thyme &time=again
" for a variable comment
. Not using encodeURIComponent
on this variable will give comment=Thyme%20&time=again
. Note that the ampersand and the equal sign mark a new key and value pair. So instead of having a POST comment
key equal to "Thyme &time=again
", you have two POST keys, one equal to "Thyme
" and another (time
) equal to again
.
For application/x-www-form-urlencoded
(POST), per http://www.w3.org/TR/html401/interac...m-content-type, spaces are to be replaced by '+', so one may wish to follow a encodeURIComponent
replacement with an additional replacement of "%20" with "+".
相关:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent
http://www.w3.org/TR/html401/interact/forms.html#form-content-type
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端