本文的目的是介绍JSURL传中文参数引发的乱码问题的详细情况,特别关注js中url中文乱码的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解JSURL传中文参数引发的乱
本文的目的是介绍JS URL传中文参数引发的乱码问题的详细情况,特别关注js中url中文乱码的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解JS URL传中文参数引发的乱码问题的机会,同时也不会遗漏关于.Net获取URL中文参数值的乱码问题解决方法总结、AJAX 中文参数 乱码问题、ajax传中文参数到后台乱码问题、ajax提交-----URL中文参数传递后的乱码问题的知识。
本文目录一览:- JS URL传中文参数引发的乱码问题(js中url中文乱码)
- .Net获取URL中文参数值的乱码问题解决方法总结
- AJAX 中文参数 乱码问题
- ajax传中文参数到后台乱码问题
- ajax提交-----URL中文参数传递后的乱码问题
JS URL传中文参数引发的乱码问题(js中url中文乱码)
今天的项目中碰到了一个乱码问题,从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 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.
.Net获取URL中文参数值的乱码问题解决方法总结
本文总结分析了.Net获取URL中文参数值的乱码问题解决方法。分享给大家供大家参考,具体如下:
解决方法:
1.设置web.config文件
<system.web> <globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" fileEncoding="gb2312" /> </system.web>
2.传递中文之前,将要传递的中文参数进行编码,在接收时再进行解码。
string Name = "中文参数"; Response.Redirect("B.aspx?Name="+Server.UrlEncode(Name)) ;
string Name = Request.QueryString["Name"]; Response.Write(Server.UrlDecode(Name)) ;
3.JS传中文参数:
function GoUrl(){ var Name = "中文参数"; location.href = "B.aspx?Name="+escape(Name) ; }
string Name = Request.QueryString["Name"]; Response.Write(Server.UrlDecode(Name)) ;
总结:
一般来说。设置web.config文件就可以了。但是如果你用 JavaScript 调用 webservice 方法的话(往webservice里面传递中文参数)。设置 web.config 文件好象无效。
或用:
Response.Redirect("test1.aspx?111="+System.Web.HttpUtility.UrlEncode("中华人明共和国")) ; //建议使用最后如果是从其他的页面获取中文参数没有乱码,那就更简单了 string message ="http://localhost/Test/test1.aspx?111="+System.Web.HttpUtility.UrlEncode("中华人明共和国");
http:
//你要获取某个页面的返回值的地址" //发送请求 HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(message) ; //接受请求 HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse() ; Stream receiveStream = myHttpWebResponse.GetResponseStream() ; StreamReader readStream = new StreamReader(receiveStream, System.Text.Encoding.GetEncoding("GB2312")) ; //此为要取页面的返回值输出的返回结果 returnValue = readStream.ReadToEnd();
更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。
希望本文所述对大家asp.net程序设计有所帮助。
- 详解ASP.NET Core WebApi 返回统一格式参数
- .NET/C#利用反射调用含ref或out参数的方法示例代码
- 浅谈Asp.net Mvc之Action如何传多个参数的方法
- Ajax提交参数的值中带有html标签不能提交成功的解决办法(ASP.NET)
- ASP.NET MVC后台参数验证的几种方式
- ASP.NET MVC传送参数至服务端详解及实例
- ASP.NET jquery ajax传递参数的实例
- 在ASP.NET 2.0中操作数据之六:编程设置ObjectDataSource的参数值
- .net core在服务器端获取api传递的参数过程
AJAX 中文参数 乱码问题
1)发送路径中的参数有中文,在服务器段接收参数值是乱码
例如:
var url="a.jsp?name=小李";
xmlHTTP.open ("post",url,true);
解决办法:
利用javascript的提供的escape()或encodeURI()方法
例如:
客户端:
var url="a.jsp?name=小李";
url=encodeURI(url);
url=encodeURI(url); //两次,很关键
/********************************************/
服务器端:
String name = request.getParameter("name");
name = java.net.URLDecoder.decode("name","UTF-8");
2)返回来的responseText或responseXML的值中含有中文是乱码
原因:AJAX在接收responseText或responseXML的值的时候是按照UTF-8的格式来解码的,如果服务器段发送的数据不是UTF-8的格式,那么接收responseText或responseXML的值有可能为乱码。
解决办法:在服务器指定发送数据的格式:
在jsp文件中:
response.setContentType("text/text;charset=UTF-8");//返回的是txt文本文件
或是
response.setContentType("text/xml;charset=UTF-8");//返回的xml文件
总结:1)ajax提交数据的格式默认为utf-8,利用javascript的提供的escape()或encodeURI()方法.在服务器端接收的时候要使用java.net.URLDecoder.decode("","UTF-8")方法进行解码.
2)xtmlhttp 返回的数据默认的字符编码是utf-8,所以服务器要向客户端发送数据的时候,也要采用utf-8编码
如果上述方法仍然解决不了乱码问题,那你尝试一下把jsp,htm,java文件用UTF-8编码格式保存.
总之:前后台数据交互都采用utf-8编码就行了.
---------------------------------
如果用escape()将中文参数编码后总是抛出java.io.CharConversionException: isHexDigit异常。
只能用encodeURL()两次编码
文章来源:http://www.jb51.cc/article/p-vxjsuigu-mk.html
ajax传中文参数到后台乱码问题
方法可能有好几种,自己mark一下自己用的
前台
window.location='/BillProductPreview.do?actiontype=AJAXOPENWORDFUND&file='+encodeURI(encodeURI(v_file));
后台
String filepath=(String) request.getParameter("file"); filepath = URLDecoder.decode(filepath,"UTF-8");
ajax提交-----URL中文参数传递后的乱码问题
jsp页面:
$.ajax({ type:'POST',url:"../user/userInfoExportExcel?org.orgId="+org_id+"&user.uesrName="+encodeURI(encodeURI(user_name)),success:function(json){ var Json = eval("(" + json.jsonResult + ")"); if(Json.result!="fail"){ window.location.href="../orgtree/exportExcel.action?fileName="+Json.result; ajaxbg.hide(); }else{ alertMsg.error('导出失败!'); ajaxbg.hide(); } } });
java后台:
if (getParameterValue("user.uesrName") != null && !getParameterValue("user.uesrName").equals("")) { String decodeUserName = java.net.URLDecoder.decode((String)getParameterValue("user.uesrName"),"utf-8"); c.andUserNameLike("%" + decodeUserName.trim() + "%"); }
我们今天的关于JS URL传中文参数引发的乱码问题和js中url中文乱码的分享已经告一段落,感谢您的关注,如果您想了解更多关于.Net获取URL中文参数值的乱码问题解决方法总结、AJAX 中文参数 乱码问题、ajax传中文参数到后台乱码问题、ajax提交-----URL中文参数传递后的乱码问题的相关信息,请在本站查询。
本文标签: