最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:动视网 责编:小采 时间:2020-11-27 21:06:43
文档

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

使用Post提交时须将空格转换成加号的解释_javascript技巧:jQuery的serialize模块中有个r20正则 代码如下: var r20 = /%20/g, jQuery.param方法中会将所有的%20转成+,即提交数据前,数据中如果包含空格,那经过encodeURIComponent后,空格会转成%20 代码如下: encodeURICompone
推荐度:
导读使用Post提交时须将空格转换成加号的解释_javascript技巧:jQuery的serialize模块中有个r20正则 代码如下: var r20 = /%20/g, jQuery.param方法中会将所有的%20转成+,即提交数据前,数据中如果包含空格,那经过encodeURIComponent后,空格会转成%20 代码如下: encodeURICompone

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技巧

使用Post提交时须将空格转换成加号的解释_javascript技巧:jQuery的serialize模块中有个r20正则 代码如下: var r20 = /%20/g, jQuery.param方法中会将所有的%20转成+,即提交数据前,数据中如果包含空格,那经过encodeURIComponent后,空格会转成%20 代码如下: encodeURICompone
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top