首页 > web前端 > js教程 > 使用Post提交时须将空格转换成加号的解释_javascript技巧

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

WBOY
发布: 2016-05-16 17:43:44
原创
1581 人浏览过

jQuery的serialize模块中有个r20正则

复制代码 代码如下:

var r20 = / /g,

jQuery.param方法中会将所有的" "转成" ",即提交数据前,数据中如果包含空格,那经过encodeURIComponent后,空格会转成" "
复制代码 代码如下:

encodeURIComponent(' ') === ' '; // true

最后需要将" "转换成"="再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 &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 " " 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
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板