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