在传参时,url参数中包含+、空格、=、%、&、#等特殊符号的处理

    最近在工作中遇到一些问题,就是如果表单中填写一些特殊字符,使用url向后台传参的时候会遇到问题,就是比如"#$%^&*()+"等特殊字符传不到后台,查了一些资料,整理下,供参考

    首先可以先将特殊字符转码为16进制的对应的转码为

  

  1. url参数中有+、空格、=、%、&、#等特殊符号的问题解决?  
  2. 解决办法:  
  3. 将这些字符转化成服务器可以识别的字符,对应关系如下:  
  4. URL字符转义  
  5.   
  6. +    URL 中+号表示空格                      %2B     
  7. 空格 URL中的空格可以用+号或者编码           %20   
  8. /   分隔目录和子目录                        %2F       
  9. ?    分隔实际的URL和参数                    %3F       
  10. %    指定特殊字符                           %25       
  11. #    表示书签                               %23       
  12. &    URL 中指定的参数间的分隔符             %26       
  13. =    URL 中指定参数的值                     %3D  
  14.   
  15. 看看实例:  
  16. <input type="button" value="aaa" id="btn_submit">  
  17. <script>  
  18. (function(){  
  19.     $("#btn_submit").click(function(){  
  20.         $.post("index.php/Layout/urlDemo",{  
  21.             id:2,  
  22.             sex:'&male'  
  23.         },function(data){  
  24.             console.log("First post:"+data);  
  25.             $.post("index.php/Layout/urlDemo?id=2&sex=&male",{},function(data){  
  26.                 console.log("Second post:"+data);  
  27.                 $.get("index.php/Layout/urlDemo",{  
  28.                     id:2,  
  29.                     sex:'&male'  
  30.                 },function(data){  
  31.                     console.log("First get:"+data);  
  32.                     $.get("index.php/Layout/urlDemo?id=2&sex=&male",{},function(data){  
  33.                         console.log("Second get:"+data);  
  34.                     });  
  35.                 });  
  36.             });  
  37.         });  
  38.     });  
  39.   
  40. })(jQuery);  
  41. </script>  
  42.   
  43. PHP中Action:  
  44. class LayoutAction extends Action {  
  45.     public function urlDemo(){  
  46.         //此处PHP并没有执行解码  
  47.         echo "id=".$_REQUEST['id'].";性别=".$_REQUEST['sex'];  
  48.     }  
  49. }  
  50. 结果如下图1:  


[html]  view plain copy print ?
  1. 转码后:  
  2. <script>  
  3. (function(){  
  4.     $("#btn_submit").click(function(){  
  5.         $.post("index.php/Layout/urlDemo",{  
  6.             id:2,  
  7.             sex:'&male'  
  8.         },function(data){  
  9.             console.log("First post:"+data);  
  10.               
  11.             $.post("index.php/Layout/urlDemo?id=2&sex="+encodeURIComponent('&male'),{},function(data){  
  12.                 console.log("Second post:"+data);  
  13.                 $.get("index.php/Layout/urlDemo",{  
  14.                     id:2,  
  15.                     sex:'&male'  
  16.                 },function(data){  
  17.                     console.log("First get:"+data);  
  18.                       
  19.                     $.get("index.php/Layout/urlDemo?id=2&sex="+encodeURIComponent('&male'),{},function(data){  
  20.                         console.log("Second get:"+data);  
  21.                     });  
  22.                 });  
  23.             });  
  24.         });  
  25.     });  
  26. })(jQuery);  
  27. </script>  
  28. 结果如下图2:  


[html]  view plain copy print ?
  1. jQuery ajax封装的get()和post(),已经对特殊字符"&"等做了处理:  
  2. 看看如下正则:  
  3. rprotocol = /^\/\//,  
  4. rquery = /\?/,  
  5. rscript = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,  
  6. rselectTextarea = /^(?:select|textarea)/i,  
  7. rspacesAjax = /\s+/,  
  8. rts = /([?&])_=[^&]*/,  
  9. rurl = /^([\w\+\.\-]+:)(?:\/\/([^\/?#:]*)(?::(\d+))?)?/;  
  10.   
  11. // try replacing _if it is there  
  12. ret = s.url.replace(rts, "$1_=" + ts);  
  13. // if nothing was replaced, add timestamp to the end  
  14. s.url = ret + ((ret === s.url) ? (rquery.test(s.url) ? "&": "?") + "_=" + ts: "");  
  15. 最后附:  
  16. javascript 编码和解码函数:  
  17.   
  18. 1)encodeURI():  
  19. a>主要用于整个URI  
  20. b>对空格进行编码  
  21. c>不会对本身属于URI的特殊字符进行编码,例如":","/","?","#"  
  22.   
  23. 2)encodeURIComponent():  
  24. a>主要用于URI中的某一段  
  25. b>会对发现的任何非标准字符进行编码  
  26.   
  27. 3)escape():  
  28. a>不会对 ASCII 字母和数字进行编码,  
  29. b>不会对下面这些 ASCII 标点符号进行编码: * @ - _ + . /   
  30. c>其他所有的字符都会被转义序列替换。  
  31. d>ECMAScript v3 反对使用该方法,应用使用 decodeURI() 和 decodeURIComponent() 替代它。  
  32. <script type="text/javascript">  
  33. //Visit%20W3@@@School%21  
  34. console.log(escape("Visit W3@@@School!"));  
  35. //%3F%21%3D%28%29%23%25%26  
  36. console.log(escape("?!=()#%&"))  
  37. </script>  


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值