使用Post提交時須將空格轉(zhuǎn)換成加號的解釋
更新時間:2013年01月14日 17:20:02 作者:
參數(shù)有中包含空格且使用Post提交時須將空格轉(zhuǎn)換成加號,這樣后臺程序接受到的才是真正的空格,感興趣的朋友可以了解下
jQuery的serialize模塊中有個r20正則
var r20 = /%20/g,
jQuery.param方法中會將所有的"%20"轉(zhuǎn)成"+",即提交數(shù)據(jù)前,數(shù)據(jù)中如果包含空格,那經(jīng)過encodeURIComponent后,空格會轉(zhuǎn)成"%20"
encodeURIComponent(' ') === '%20'; // true
最后需要將"%20"轉(zhuǎn)換成"="再Post提交。這樣后臺程序接受到的才是真正的空格。
關(guān)于 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 "+".
相關(guān):
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent
http://www.w3.org/TR/html401/interact/forms.html#form-content-type
復(fù)制代碼 代碼如下:
var r20 = /%20/g,
jQuery.param方法中會將所有的"%20"轉(zhuǎn)成"+",即提交數(shù)據(jù)前,數(shù)據(jù)中如果包含空格,那經(jīng)過encodeURIComponent后,空格會轉(zhuǎn)成"%20"
復(fù)制代碼 代碼如下:
encodeURIComponent(' ') === '%20'; // true
最后需要將"%20"轉(zhuǎn)換成"="再Post提交。這樣后臺程序接受到的才是真正的空格。
關(guān)于 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 "+".
相關(guān):
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent
http://www.w3.org/TR/html401/interact/forms.html#form-content-type
相關(guān)文章
sessionStorage存儲時多窗口之前能否進行狀態(tài)共享解析
這篇文章主要為大家介紹了sessionStorage存儲時多窗口之前能否進行狀態(tài)共享解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
JavaScript中Infinity(無窮數(shù))的使用和注意事項
Infinity(無窮大)在 JS 中是一個特殊的數(shù)字,它的特性是它比任何有限的數(shù)字都大,如果不知道 Infinity,我們在一些運算操作遇到時,就會覺得很有意思,下面這篇文章主要給大家介紹了關(guān)于JavaScript中Infinity(無窮數(shù))的使用和注意事項,需要的朋友可以參考下2022-04-04
bootstrap fileinput實現(xiàn)文件上傳功能
這篇文章主要為大家詳細介紹了bootstrap fileinput實現(xiàn)文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
前端實現(xiàn)下載文件(包含壓縮包下載)方式詳細總結(jié)
這篇文章主要給大家介紹了關(guān)于前端實現(xiàn)下載文件(包含壓縮包下載)方式的相關(guān)資料,這段時間項目需要下載文件,所以這里給大家總結(jié)下,需要的朋友可以參考下2023-09-09
微信小程序?qū)崿F(xiàn)樹莓派(raspberry pi)小車控制
這篇文章主要為大家詳細介紹了微信小程序?qū)崿F(xiàn)樹莓派(raspberry pi)小車控制,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02

