參數有中包含空格且使用Post提交時須將空格轉換成加號


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM