今天的项目中碰到了一个乱码问题,从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地址,在后台处理时会发生转换错误。在有些传递页面使用GB2312,而在接收页面使用 UTF8,这样接收到的参数就可能会与原来发生不一致。使用服务器端的urlEncode函数编码的URL,与使用客户端javascript的 encodeURI函数编码的URL,结果就不一样。
javaScript中的编码方法:
escape() 方法:
采用 ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符 在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。unescape方法与此相反。不会被此方法编码的字符: @ * / +英 文解释: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 characterencodeURIComponent() 方法:把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.

转载于:https://www.cnblogs.com/xuxiaoshuan/p/4519522.html

JS URL传中文参数引发的乱码问题相关推荐

  1. java url传参中文乱码问题_java中url传中文参数出现乱码

    缘由:java Http请求传输时将url以ISO-8859-1编码,服务器收到字节流后默认会以ISO-8859-1编码来解码成字符流(形成中文乱码).服务器 方法一:编码 咱们须要把request. ...

  2. Java中url传递中文参数取值乱码的解决方法

    Java中url传递中文参数取值乱码的解决方法 参考文章: (1)Java中url传递中文参数取值乱码的解决方法 (2)https://www.cnblogs.com/liwenjuan/p/3211 ...

  3. HTML的Get方法URL传递中文参数,解决乱码问题

    HTML的Get方法URL传递中文参数,解决乱码问题 参考文章: (1)HTML的Get方法URL传递中文参数,解决乱码问题 (2)https://www.cnblogs.com/chenyucong ...

  4. JSP中文及传中文参数乱码解决方法小结

    在使用JSP的过程中,最使人头疼的一个问题就是中文乱码问题,以下是我在软件开发中遇到的乱码问题以及解决方法. 1.JSP页面乱码 这种乱码的原因是应为没有在页面里指定使用的字符集编码,解决方法:只要在 ...

  5. js传中文参数 java取_js中文转码传输java后台 适用于用url传递中文参数

    第一种方法: 解决方法如下: 1.在JS里对中文参数进行两次转码 var login_name = document.getElementById("loginname").val ...

  6. split逗号分割中文出现乱码java_Java中URL传中文时乱码的问题解决方案

    前言 Java中URL传中文时乱码的问题相信不少朋友都遇到过,最近就遇到一个问题,就是在Action当中把一条中文信息绑定在URL的后面,ActionForward到别一个页面时,用reqeust.g ...

  7. jsp页面之间传中文参数显示乱码问题的解决

    jsp页面之间传中文参数显示乱码问题的解决 参考文章: (1)jsp页面之间传中文参数显示乱码问题的解决 (2)https://www.cnblogs.com/sllzhj/p/9673628.htm ...

  8. 微信小程序页面跳转,url传参参数丢失问题

    微信小程序页面跳转,url传参参数丢失问题 // pages/order/purchase/index.js// 跳转到采购订单详情toPurchaseOrderDetail(e) {// conso ...

  9. url 编码 js url传参中文乱码解决方案

    前后台用js传参过程中,如果是中文就容易出现乱码,所以最好是先编码. 1.配置文件web.config中 在节中加上整个网站的编码方式.  <globalization fileEncoding ...

  10. IE浏览器url带中文参数导致乱码问题(chrome下正常)

    问题: E浏览器下url带中文请求参数,服务器端使用new String(param.getBytes("iso-8859-1"), "utf-8")后仍然会乱 ...

最新文章

  1. 使用ssh工具链接mysql_mycli辅助工具-更方便得通过ssh tunnel连接线上MySQL
  2. Fvwm-背景图片设置三法
  3. ztree获取勾选节点数据并且与表单信息合并
  4. 关于scriptManager与JS代码兼容问题
  5. python 字典添加元素
  6. 如何使用GZip和Jersey压缩Java REST API中的响应
  7. React使用antd Table生成层级多选组件
  8. 移动端取消iphone ipad默认按钮
  9. virtual box一直正在加载文件_如何用Band-in-a-box进行民乐编曲
  10. 【转】用Qt生成dll类库及调用方法
  11. GNU Emacs的终极扩展管理工具 — el-get
  12. 天然黑糖行业调研报告 - 市场现状分析与发展前景预测
  13. abstract 抽象类
  14. Synchronized快
  15. x550网卡linux驱动,intel_I350_I354_X520_X540_X550_intel网卡驱动_NIC驱动下载_5分享
  16. 使用VUE脚手架搭建VUE项目
  17. 量化交易----常见股票特征和编程实现
  18. 编程都是人上人,果不其然!2020年度十大高薪岗位,程序员独领风骚!
  19. 利用HomeKit、智汀家庭云,让不同生态智能家居实现互联互通
  20. 探讨OC的内存管理 以及防止循环引用retain cycle 代理为什么用weak block为什么用copy

热门文章

  1. 今天给一份 2022 互联网就业指南。
  2. 国内开源落后?那是不是要做点什么。
  3. Linux进阶之Jenkins持续集成介绍及安装演示
  4. Make jQuery throw error when it doesn't match an element
  5. P1020 导弹拦截 dp 树状数组维护最长升序列
  6. struts转换器详解
  7. LeetCode Shortest Word Distance II
  8. Android实现传感器应用及位置服务
  9. Asp.Net MVC 模型(使用Entity Framework创建模型类)1
  10. jQuery mobile 开发问题记录