手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >使用Post提交时须将空格转换成加号的解释
使用Post提交时须将空格转换成加号的解释
摘要:jQuery的serialize模块中有个r20正则复制代码代码如下:varr20=/%20/g,jQuery.param方法中会将所有的"...

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

【使用Post提交时须将空格转换成加号的解释】相关文章:

JavaScript 定时器 SetTimeout之定时刷新窗口和关闭窗口(代码超简单)

百度地图给map添加右键菜单(判断是否为marker)

基于Web标准的UI组件 — 树状菜单(2)

文字幻灯片

js实现键盘Enter键提交表单的方法

Javascript节点关系实例分析

js光标定位文本框回车表单提交问题的解决方法

多种js图片预加载实现方式分享

AspNet中使用JQuery boxy插件的确认框

JavaScript每天定时更换皮肤样式的方法

精品推荐
分类导航