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

解决方法如下:

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方法与此相反。不会被此方法编码的字符: @ * / +

如果是gb2312编码的可以使用escape,不能用encodeURIComponent,要不会乱码。

escape的使用方法:http://www.jb51.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.

(0)

相关推荐

  • js将json格式的对象拼接成复杂的url参数方法

    var parseParam=function(param, key){ varparamStr=""; if(paraminstanceof String||param instanceof Number||param instanceof Boolean){ paramStr+="&"+key+"="+encodeURIComponent(param); }else{ $.each(param,function(i){ vark=ke

  • JS根据key值获取URL中的参数值及把URL的参数转换成json对象

    不废话了,直接贴代码了,通过示例一讲解JS根据key值获取URL中的参数值及把URL的参数转换成json对象,示例二讲解js获取url传递参数,具体内容请看下文 示例一: //把url的参数部分转化成json对象 parseQueryString: function (url) { var reg_url = /^[^\?]+\?([\w\W]+)$/, reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g, arr_url = reg_url.exec(url

  • js的form表单提交url传参数(包含+等特殊字符)的两种解决方法

    方法一:(伪装form表单提交) linkredwin = function(A,B,C,D,E,F,G){ var formredwin = document.createElement("form"); formredwin.method = 'POST'; document.body.appendChild(formredwin); formredwin.action = "http://www.A.com/A.wiki?A=" +encodeURI(A) +

  • 一个JavaScript函数把URL参数解析成Json对象

    问题:请编写一个JavaScript函数parseQueryString,它的用途是把URL参数解析为一个对象. eg:var obj=parseQueryString(url); 创建对象的三种形式: 一: var Person=new Object(); Person.name="Sun"; Person.age=24; 二: var Person=new Object(); Person["name"]="Sun"; Person[&quo

  • 四种参数传递的形式——URL,超链接,js,form表单

    什么时候用GET,  查,删 什么时候用POST,增,改  (特列:登陆用Post,因为不能让用户名和密码显示在URL上) 4种get传参方式 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>&l

  • JavaScript获取URL参数的方法之一

    若地址栏URL为:abc.html?m=tomms&c=allsearchlist&pageNo=1&pageNum=20&text=1 <script> //JavaScript获取url,并把url中的参数变成数组的方法,arr数组的值就是各参数值 var url = window.document.location.href.toString(); var u = url.split("?"); var arr = []; if(typ

  • javascript将url中的参数加密解密代码

    今天在做一个老项目时,遇到一个需求,在javascript将url中的参数加密解密,从网上找发现了这段有用的代码: 复制代码 代码如下: <SCRIPT LANGUAGE="JavaScript">    <!-- Begin    function Encrypt(str, pwd) {        if(str=="")return "";        str = escape(str);        if(!pwd

  • js获取url参数值的两种方式

    方法一:正则分析法 复制代码 代码如下: function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return n

  • nodejs实现获取当前url地址及url各种参数值

    复制代码 代码如下: //需要使用的模块 http   url 当前url   http://localhost:8888/select?aa=001&bb=002 var http = require('http'); var URL = require('url'); http.createServer(function(req, res){    var arg = url.parse(req.url).query;  //方法一arg => aa=001&bb=002   

  • jsp中URL传递中文参数的处理方法

    在页面的url中使用encodeURI(encodeURI(中文)),对中文进行编码,并在服务器的java程序中使用URLDecoder.decode(中文, "UTF-8")进行解码即可; 如果url中需要传递+.#.?等特殊符号,可以使用encodeURIComponent(encodeURIComponent(中文)),服务器解码方法跟encodeURI的解码相同.

  • 使用JavaScript获取URL中的参数(两种方法)

    本文给大家分享两种方法使用js获取url中的参数,其中方法二是使用的正则表达式方法,大家可以根据需要选择比较好的方法,废话不多说了,直接看详细介绍吧. 方法一: //取url参数 var type = request("type") function request() { var query = location.search; var paras = arguments[0]; if (arguments.length == 2) { query = arguments[1]; }

随机推荐