最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS URL傳中文參數引發(fā)的亂碼問題

 更新時間:2009年09月02日 20:43:15   投稿:mdxy-dxy  
今天的項目中碰到了一個亂碼問題,從JS里傳URL到服務器,URL中有中文參數,服務器里讀出的中文參數來的全是“?”,查了網上JS編碼相關資料得以解決。

解決方法如下:

1、在JS里對中文參數進行兩次轉碼

復制代碼 代碼如下:

var login_name = document.getElementById("loginname").value;
login_name = encodeURI(login_name);
login_name = encodeURI(login_name);

2、在服務器端對參數進行解碼
復制代碼 代碼如下:

String loginName = ParamUtil.getString(request, "login_name");
loginName = java.net.URLDecoder.decode(loginName,"UTF-8");


在使用url進行參數傳遞時,經常會傳遞一些中文名的參數或URL地址,在后臺處理時會發(fā)生轉換錯誤。在有些傳遞頁面使用GB2312,而在接收頁面使用UTF8,這樣接收到的參數就可能會與原來發(fā)生不一致。使用服務器端的urlEncode函數編碼的URL,與使用客戶端javascript的encodeURI函數編碼的URL,結果就不一樣。

javaScript中的編碼方法:

escape() 方法:
采用ISO Latin字符集對指定的字符串進行編碼。所有的空格符、標點符號、特殊字符以及其他非ASCII字符都將被轉化成%xx格式的字符編碼(xx等于該字符在字符集表里面的編碼的16進制數字)。比如,空格符對應的編碼是%20。unescape方法與此相反。不會被此方法編碼的字符: @ * / +

如果是gb2312編碼的可以使用escape,不能用encodeURIComponent,要不會亂碼。

escape的使用方法:http://www.fzitv.net/w3school/jsref/jsref_escape.htm

英文解釋:MSDN JScript Reference: The escape method returns a string value (in Unicode format) that contains the contents of [the argument]. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as "%20."
Edge Core Javascript Guide: The escape and unescape functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The unescape function returns the ASCII string for the specified hexadecimal encoding value.


encodeURI() 方法:把URI字符串采用UTF-8編碼格式轉化成escape格式的字符串。不會被此方法編碼的字符:! @ # $& * ( ) = : / ; ? + '

英文解釋:MSDN JScript Reference: The encodeURI method returns an encoded URI. If you pass the result to decodeURI, the original string is returned. The encodeURI method does not encode the following characters: ":", "/", ";", and "?". Use encodeURIComponent to encode these characters. Edge Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character


encodeURIComponent() 方法:把URI字符串采用UTF-8編碼格式轉化成escape格式的字符串。與encodeURI()相比,這個方法將對更多的字符進行編碼,比如 / 等字符。所以如果字符串里面包含了URI的幾個部分的話,不能用這個方法來進行編碼,否則 / 字符被編碼之后URL將顯示錯誤。不會被此方法編碼的字符:! * ( )

英文解釋:MSDN JScript Reference: The encodeURIComponent method returns an encoded URI. If you pass the result to decodeURIComponent, the original string is returned. Because the encodeURIComponent method encodes all characters, be careful if the string represents a path such as /folder1/folder2/default.html. The slash characters will be encoded and will not be valid if sent as a request to a web server. Use the encodeURI method if the string contains more than a single URI component. Mozilla Developer Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.



因此,對于中文字符串來說,如果不希望把字符串編碼格式轉化成UTF-8格式的(比如原頁面和目標頁面的charset是一致的時候),只需要使用escape。如果你的頁面是GB2312或者其他的編碼,而接受參數的頁面是UTF-8編碼的,就要采用encodeURI或者encodeURIComponent。

另外,encodeURI/encodeURIComponent是在javascript1.5之后引進的,escape則在javascript1.0版本就有。

英文注釋:The escape() method does not encode the + character which is interpreted as a space on the server side as well as generated by forms with spaces in their fields. Due to this shortcoming, you should avoid use of escape() whenever possible. The best alternative is usually encodeURIComponent().Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs [REF] as opposed to the querystring, which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. Note that this method does not encode the ' character, as it is a valid character within URIs.Lastly, the encodeURIComponent() method should be used in most cases when encoding a single component of a URI. This method will encode certain chars that would normally be recognized as special chars for URIs so that many components may be included. Note that this method does not encode the ' character, as it is a valid character within URIs.

相關文章

  • 微信小程序實現的一鍵撥號功能示例

    微信小程序實現的一鍵撥號功能示例

    這篇文章主要介紹了微信小程序實現的一鍵撥號功能,結合實例形式分析了微信小程序使用wx.makePhoneCall實現撥打電話功能相關操作技巧,需要的朋友可以參考下
    2019-04-04
  • 帶有定位當前位置的百度地圖前端web api實例代碼

    帶有定位當前位置的百度地圖前端web api實例代碼

    這篇文章主要介紹了帶有定位當前位置的百度地圖前端web api實例代碼 的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • 詳解微信小程序框架wepy踩坑記錄(與vue對比)

    詳解微信小程序框架wepy踩坑記錄(與vue對比)

    這篇文章主要介紹了詳解微信小程序框架wepy踩坑記錄(與vue對比),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • 用Golang運行JavaScript的實現示例

    用Golang運行JavaScript的實現示例

    這篇文章主要介紹了用Golang運行JavaScript的實現示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • php結合js實現多條件組合查詢

    php結合js實現多條件組合查詢

    這篇文章主要為大家詳細介紹了php結合js實現多條件組合查詢,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • 原生JS實現輪播圖效果

    原生JS實現輪播圖效果

    這篇文章主要為大家詳細介紹了原生JS實現輪播圖效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • 小程序ios音頻播放沒聲音問題的解決

    小程序ios音頻播放沒聲音問題的解決

    這篇文章主要介紹了小程序ios音頻播放沒聲音問題的解決,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • JavaScript 短路運算的實現

    JavaScript 短路運算的實現

    本文主要介紹了JavaScript 短路運算的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • 淺析如何在Bash中調用Node運行JS文件進行數據通信

    淺析如何在Bash中調用Node運行JS文件進行數據通信

    這篇文章主要來和大家探討在 Bash 中調用 Node 運行 JS 文件時如何進行數據通信,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-03-03
  • javascript中的undefined 與 null 的區(qū)別  補充篇

    javascript中的undefined 與 null 的區(qū)別 補充篇

    在Javascript中有兩個值用來代表類似空值的概念,undefined和null,這兩個很容易被混淆,他們表示的是兩個不同的概念。
    2010-03-03

最新評論

泸州市| 江西省| 古田县| 曲松县| 瑞安市| 五峰| 隆化县| 黄大仙区| 弥渡县| 江达县| 沁水县| 蒙山县| 阿瓦提县| 五寨县| 姚安县| 黄骅市| 九江县| 吴旗县| 新余市| 谷城县| 平定县| 双鸭山市| 湖口县| 东明县| 静海县| 浦江县| 蒲江县| 西林县| 兴海县| 红原县| 县级市| 石泉县| 奇台县| 玉溪市| 聂荣县| 雅江县| 雷州市| 邵阳市| 东乌珠穆沁旗| 彰化市| 若尔盖县|