Ajax get方式传值乱码问题
我的解决方式是:
在tomcat的server.xml配置文件里追加URIEncoding="UTF-8":
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"
URIEncoding="UTF-8"/>
再对需要传递的中文先进行编码:encodeURIComponent('你好');
<script type="text/javascript">
function getText(){
var xhr = getXhr();
var hello = encodeURIComponent('你好');
xhr.open("get","testEncode.do?str="+hello,true);
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
alert(xhr.responseText);
}
}
xhr.send();
}
</script>
在后台直接用request.getParameter("str")取即可。
Never frown, even when you are sad, because you never know who is falling in love with your smile.
纵然伤心,也不要愁眉不展,因为你不知道谁会爱上你的笑容。

浙公网安备 33010602011771号