JS中URL传中文参数引发的乱码问题

开发者福利!热门AI工具限时免费用 购周边即赠Coding Plan Lite,Claude Code、Cursor等20+工具畅享,效率翻倍! 阅读详情

今天的项目中碰到了一个乱码问题,从JS里传URL到服务器,URL中有中文参数,服务器里读出的中文参数来的全是“?”,查了网上JS编码相关资料得以解决。

解决方法如下:

1、在JS里对中文参数进行两次转码

2、在服务器端对参数进行解码

 

附:(javaScript中的编码方法

注:摘自http://www.cnblogs.com/winner/archive/2007/08/28/873498.html

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 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.

JavaScript中Unicode转码方法详解 JavaScript中,处理Unicode编码是很常见的任务之一。本文将详细介绍JavaScript中的Unicode转码方法,并提供相应的源代码示例。要将字符串转换为Unicode编码,可以使用JavaScript的charCodeAt()方法。方法获取每个字符的Unicode编码,然后将其转换为16进制表示,并添加到。最后,将字符拼接成完整的字符串,并返回结果。如果要将Unicode编码转换回字符串,可以使用JavaScript的。函数接受一个字符串作为参数,并遍历字符串中的每个字符。 阅读详情

相关推荐

encoding.js:转换或检测JavaScript中的字符编码

encoding.js 转换JavaScript中的字符编码。 安装 在浏览器中: < script src =" encoding.js " > </ script > 或者 < script src =" encoding.min.js " > </ script > 对象编码将在全局范围内定义。 数组的转换和检测(如Array对象)。 在Node.js中: encoding.js是在npm中按encoding-japanese的模块名称发布的。 npm install encoding-japanese var encoding = require ( 'encoding-japanese' ) ; 每种方法也可用于Node.js中的Buffer 。 凉亭: bower install encoding-japanese CDN encoding.js在上。 转

jsurl进行编码解码(三种方式)

escape官方已不推荐使用了,基本淘汰了,如果想对URL编码,最好不要使用此方法,encodeURI和比较常用。前者假定它的参数是 URI 的一部分(比如协议、主机名、路径或查询字符串)。因此 encodeURIComponent() 函数将转义用于分隔 URI 各个部分的标点符号。如果文章对你有所帮助,!博主会持续更新。。。。

富朝阳的博客 3万+

JSUrlEncode

JSUrlEncode 和 UrlDecode 函数

Js中对URL进行转码与解码

escape 和 unescape escape()不能直接用于URL编码,它的真正作用是返回一个字符的Unicode编码值。 采用unicode字符集对指定的字符串除0-255以外进行编码。所有的空格符、标点符号、特殊字符以及更多有联系非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。 escape不编码字符有...

日常爱喝茶,闲时聊科技 2335

JS URL中文参数引发乱码问题

项目中碰到了一个乱码问题,从JSURL到服务器,URL中有中文参数,服务器里读出的中文参数来的全是“?”,查了网上JS编码相关资料得以解决。 解决方法如下: 1、在JS里对中文参数进行两次转码 var login_name = document.getElementById("loginname").value; login_name = encodeURI(login_n

夜礼服假面 4005

js java乱码问题_JS URL中文参数引发乱码问题

今天的项目中碰到了一个乱码问题,从JSURL到服务器,URL中有中文参数,服务器里读出的中文参数来的全是“?”,查了网上JS编码相关资料得以解决。解决方法如下:1、在JS里对中文参数进行两次转码代码如下:var login_name = document.getElementById("loginname").value;login_name = encodeURI(login_name);l...

weixin_28737603的博客 197

js java乱码_JS URL中文参数引发乱码问题

今天的项目中碰到了一个乱码问题,从JSURL到服务器,URL中有中文参数,服务器里读出的中文参数来的全是“?”,查了网上JS编码相关资料得以解决。解决方法如下:1、在JS里对中文参数进行两次转码代码如下:var login_name = document.getElementById("loginname").value;login_name = encodeURI(login_name);l...

weixin_39600447的博客 161
上一篇: 第一个DWR程序遇到的问题
下一篇: mysql发生系统错误1067
Seven_23
博客等级 码龄17年 5粉丝 10原创
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值