使用Post提交时须将空格转换成加号的解释


Posted in Javascript onJanuary 14, 2013

jQuery的serialize模块中有个r20正则

var r20 = /%20/g,

jQuery.param方法中会将所有的"%20"转成"+",即提交数据前,数据中如果包含空格,那经过encodeURIComponent后,空格会转成"%20"
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

Javascript 相关文章推荐
javascript中length属性的探索
Jul 31 Javascript
正则表达式搭配js轻松处理json文本方便而老古
Feb 17 Javascript
js网页实时倒计时精确到秒级
Feb 10 Javascript
使用window.prompt()实现弹出用户输入的对话框
Apr 13 Javascript
详解javascript传统方法实现异步校验
Jan 22 Javascript
用canvas 实现个图片三角化(LOW POLY)效果
Feb 18 Javascript
用js写的一个路由(简单实例)
Sep 24 Javascript
JavaScript实现二叉树的先序、中序及后序遍历方法详解
Oct 26 Javascript
基于vue-ssr的静态网站生成器VuePress 初体验
Apr 17 Javascript
详解ES6系列之私有变量的实现
Nov 21 Javascript
详解vue-property-decorator使用手册
Jul 29 Javascript
关于对TypeScript泛型参数的默认值理解
Jul 15 Javascript
javascript函数以及基础写法100多条实用整理
Jan 13 #Javascript
window.requestAnimationFrame是什么意思,怎么用
Jan 13 #Javascript
jquery获取div宽度的实现思路与代码
Jan 13 #Javascript
jQuery实现用方向键控制层的上下左右移动
Jan 13 #Javascript
js模仿html5 placeholder适应于不支持的浏览器
Jan 13 #Javascript
ie6下png图片背景不透明的解决办法使用js实现
Jan 11 #Javascript
关于全局变量和局部变量的那些事
Jan 11 #Javascript
You might like
PHP截取中文字符串的问题
2006/07/12 PHP
php获取CSS文件中图片地址并下载到本地的方法
2014/12/02 PHP
CodeIgniter视图使用注意事项
2016/01/20 PHP
PHP获取网页所有连接的方法(附demo源码下载)
2016/03/30 PHP
在PHP中输出JS语句以及乱码问题的解决方案
2019/02/13 PHP
PHP实现会员账号单唯一登录的方法分析
2019/03/07 PHP
Javascript中的delete介绍
2012/09/02 Javascript
appendChild() 或 insertBefore()使用与区别介绍
2013/10/11 Javascript
javascript异步处理工作机制详解
2015/04/13 Javascript
JS根据key值获取URL中的参数值及把URL的参数转换成json对象
2015/08/26 Javascript
jQuery animate和CSS3相结合实现缓动追逐效果附源码下载
2016/04/18 Javascript
可输入文字查找ajax下拉框控件 ComBox的实现方法
2016/10/25 Javascript
Ajax的概述与实现过程
2016/11/18 Javascript
详解如何提升JSON.stringify()的性能
2019/06/12 Javascript
[35:34]Liquid vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
详解将Django部署到Centos7全攻略
2018/09/26 Python
值得收藏,Python 开发中的高级技巧
2018/11/23 Python
使用python打印十行杨辉三角过程详解
2019/07/10 Python
解决Python对齐文本字符串问题
2019/08/28 Python
python使用pip安装模块出现ReadTimeoutError: HTTPSConnectionPool的解决方法
2019/10/04 Python
Python倒排索引之查找包含某主题或单词的文件
2019/11/13 Python
详解使用python3.7配置开发钉钉群自定义机器人(2020年新版攻略)
2020/04/01 Python
Python实现打包成库供别的模块调用
2020/07/13 Python
Python自动巡检H3C交换机实现过程解析
2020/08/14 Python
canvas中普通动效与粒子动效的实现代码示例
2019/01/03 HTML / CSS
香港卓悦化妆品官网:BONJOUR
2017/09/21 全球购物
Juice Beauty官网:有机美容产品,护肤与化妆品
2020/06/13 全球购物
仓库主管岗位职责
2014/03/02 职场文书
六五普法规划实施方案
2014/03/21 职场文书
推荐信怎么写
2014/05/09 职场文书
公务员上班玩游戏检讨书
2014/09/17 职场文书
先进工作者推荐材料
2014/12/23 职场文书
葬礼主持词
2015/07/02 职场文书
浙江省杭州市平均工资标准是多少?
2019/07/09 职场文书
MySQL中一条SQL查询语句是如何执行的
2022/04/08 MySQL
MySQL 条件查询的常用操作
2022/04/28 MySQL