ajax检证用户就否存在和解决乱码问题!
写了一个ajax,在向服务器端发送数据时产生乱码。。在查阅一些资料后,通过测试!^_^。。。 <script> var XMLHttpReq=false; function checkProviderName(){ if( "" == DWRUtil.getValue('name').trim() ){ alert("服务商全称不能为空"); ProviderForm.name.focus(); return false; }else if( validateCN(DWRUtil.getValue('name').trim()) ){ alert("服务商全称输入错误"); ProviderForm.name.focus(); return false; }else if( validateSafe(DWRUtil.getValue('name').trim())){ alert('服务商全称不要使用非法字符!'); ProviderForm.name.focus(); return false; }
if(window.XMLHttpRequest){ //Mozilla XMLHttpReq=new XMLHttpRequest();
}else if(window.ActiveXObject){ try{ XMLHttpReq=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ XMLHttpReq=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){} } } var temp=DWRUtil.getValue('name').trim();
var url="<%=request.getContextPath()%>/action/service_provider.do?act=checkProviderName&name="+encodeURI(encodeURI(temp));//要执行两次的encodeURI////关键!!!!!
XMLHttpReq.open("GET",url,true); XMLHttpReq.;
XMLHttpReq.setrequestheader("cache-control","no-cache");
XMLHttpReq.setrequestheader("Content-Type","text/html; encoding=UTF-8");
XMLHttpReq.send(null); } function checkP(){ if(XMLHttpReq.readyState==4){ //对象状态 if(XMLHttpReq.status==200){//信息已成功返回,开始处理信息 <!--测试读取xml开始--> var root=XMLHttpReq.responseXML; var res=root.getElementsByTagName("res")[0].firstChild.data; window.alert(res);
<!--测试读取xml结束--> }else{ window.alert("所请求的页面有异常"); } } } </script>
action. public ActionForward checkProviderName( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{ ProviderForm providerForm =(ProviderForm)form; response.setContentType("text/xml; charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<response>"); String strNameEn = java.net.URLDecoder.decode(providerForm.getName(), "UTF-8");////解码关键!!!! System.out.println("sname+++++++++++>>"+strNameEn); if(checkProviderName(strNameEn)){ out.println("<res>" + "可以注册!" + "</res>"); }else{ out.println("<res>" + "不可以注册!" + "</res>"); } out.println("</response>"); return null;
|
出处:http://aaron-ch.javaeye.com/blog/80160
关键字: Ajax,URI encode
escape():
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."
encodeURI():
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.
encodeURIComponet():
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
When to use which?
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 and the fact that this function fails to handle non-ASCII characters correctly, you should avoid use of escape()
whenever possible. The best alternative is usually encodeURIComponent()
.
escape()
will not encode: @*/+
Use of the encodeURI()
method is a bit more specialized than escape()
in that it encodes for URIs 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.
encodeURI()
will not encode: ~!@#$&*()=:/,;?+'
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.
encodeURIComponent()
will not encode: ~!*()'
对比 javascript url编码
原文:http://www.javaeye.com/topic/198530
escape() 方法: 采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。
不会被此方法编码的字符: @ * / +
encodeURI() 方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。
不会被此方法编码的字符:! @ # $& * ( ) = : / ; ? + '
encodeURIComponent() 方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。与encodeURI()相比,这个方法将对更多的字符进行编码,比如 / 等字符。所以如果字符串里面包含了URI的几个部分的话,不能用这个方法来进行编码,否则 / 字符被编码之后URL将显示错误。
不会被此方法编码的字符:! * ( ) '
因此,对于中文字符串来说,如果不希望把字符串编码格式转化成UTF-8格式的(比如原页面和目标页面的charset是一致的时候),只需要使用escape。如果你的页面是GB2312或者其他的编码,而接受参数的页面是UTF-8编码的,就要采用encodeURI或者encodeURIComponent。
另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在javascript1.0版本就有。
为什么要连续两次调用 encodeURI(String) 方法?
原文:http://hi.baidu.com/zhuguoneng/blog/item/96ca9c1f3d67a966f724e465.html
是因为 Java 中的 request.getParameter(String) 方法会进行一次 URI 的解码过程,调用时内置的解码过程会导致乱码出现。
而 URI 编码两次后, request.getParameter(String) 函数得到的是原信息 URI 编码一次的内容。
接着用 java.net.URLDecoder.decode(String str,String codename) 方法,将已经编码的 URI 转换成原文。
分享到:
相关推荐
URL传中文参数导致乱码的解决方案之encodeURI
今天终于解决了AJAX的中文乱码问题,写篇文章来帮助一下...今天把问题解决了,很简单,是使用escape(或encodeURI,两个函数javascript的函数,功能基本相同,可以查一下相关的帮助),但要使用两次,这是解决问题的关键
javaScript encodeURI中文乱码
escape()、encodeURI()、encodeURIComponent()区别详解
AJAX中文问题分为两大类: 1)发送路径中的参数有中文,在服务器段接收参数值是乱码 例如: var url=”a.jsp?name=小李”; xmlHTTP.open (“post”,url,true); 解决办法: 利用javascript的提供的escape()或...
NULL 博文链接:https://liuzidong.iteye.com/blog/829204
encodeURI和encodeURIComponent以及escape,这三个都是用来编码的,本篇文章给大家介绍encodeURI和encodeURIComponent以及escape的区别与应用,感兴趣的朋友一起学习吧
escape(), encodeURI()和encodeURIComponent()是在Javascript中用于编码字符串的三个常用的方法,而他们之间的异同却困扰了很多的Javascript初学者,今天我就在这里对这三个方法详细地分析与比较一下。
简单来说,escape是对字符串(string)进行编码(而另外两种是对URL),作用是让它们在所有电脑上可读。 编码之后的效果是%XX或者%uXXXX这种形式。 其中 ASCII字母、数字、@*/+ ,这几个字符不会被编码,其余的都会。 最...
1.传递参数: 代码如下: var pmt = ‘sensor=’+ encodeURI(encodeURI(sensor))+’&device=’+encodeURI(encodeURI(device))+’&instrument=’; pmt += encodeURI(encodeURI(instrument))+’&n=’+n+’&addDate=’+...
JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape, decodeURI, decodeURIComponent 。 下面简单介绍一下它们的区别: 1 escape()函数 定义和...
Javascript中escape(),_encodeURI()和encodeURIComponent()之精析与比较.doc
使用Ajax传递中文参数,如果不对参数进行处理的话,传到后台会变成乱码,经测试可以使用如下方法 这里是以jquery.autocomplete插件中获取输入框中的数据传到后台为例: 注意要对中文使用encodeURI编码两次 代码如下:...
而他们之间的异同却困扰了很多的Javascript初学者,这篇文章详细的给大家介绍了js中编码函数:escape,encodeURI与encodeURIComponent的相关资料,需要的朋友可以参考下。
JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数: unescape,decodeURI,decodeURIComponent 。 下面简单介绍一下它们的区别 1 escape()函数 定义和...
encodeURI和encodeURIComponet函数都是javascript中用来对URI进行编码,将相关参数转换成UTF-8编码格式的数据。URI在进行定位跳转时,参数里面的中文、日文等非ASCII编码都会进行编码转换
encodeURI(URIString) 必选的 URIString 参数代表一个已编码的 URI。 说明 encodeURI 方法返回一个编码的 URI。如果您将编码结果传递给 decodeURI,那么将返回初始的字符串。encodeURI 方法不会对下列字符进行编码...