源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-03-28 08:29 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:使用Post提交时须将空格转换成加号的解释
jQuery的serialize模块中有个r20正则
[u]复制代码[/u] 代码如下:
var r20 = /%20/g,
jQuery.param方法中会将所有的"%20"转成"+",即提交数据前,数据中如果包含空格,那经过encodeURIComponent后,空格会转成"%20"
[u]复制代码[/u] 代码如下:
encodeURIComponent(' ') === '%20'; // true
最后需要将"%20"转换成"="再Post提交。这样后台程序接受到的才是真正的空格。 [b]关于 encodeURIComponent,见MDC描述 [/b] 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 "+". [b]相关[/b]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent http://www.w3.org/TR/html401/interact/forms.html#form-content-type
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部