AJAX/JSP 关于服务端返回中文乱码的问题…………

SOULisy 2007-10-02 09:07:30
各种方法都试过了,服务端返回的中文还是乱码,各位大虾给帮帮忙阿~~~

"ajax.jsp"
<%@ page contentType = "text/html;charset=UTF-8" language = "java" %>

<html>
<head>
<META http-equiv ="Content-Type" content = "text/html; charset=UTF-8" >
</head>
<script type="text/javascript" >
var XMLHttpReq = false;

//creat XMLHttpRequest
function createXMLHttpRequest(){
if(window.XMLHttpRequest){ //Mozilla
XMLHttpReq = new XMLHttpRequest();
}else if(window.ActiveXObject){ //IE
try{
XMLHttpReq = new ActiveXObject("Msxml2.XMLHttp");
}catch(e){
try{
XMLHttpRequest = new ActiveXObject("Microsoft.XMLHttp");
}catch(e){
}
}
}
}

function sendRequest(url){
createXMLHttpRequest();
XMLHttpReq.open("GET", url, true);
XMLHttpReq.onreadystatechange = processResponse;
XMLHttpReq.send(null);
}

function processResponse(){
if(XMLHttpReq.readyState == 4){
if(XMLHttpReq.status == 200){
try{
var xmlDOM = XMLHttpReq.responseXML;
var root = xmlDOM.documentElement;
var info = root.getElementsByTagName('res');
window.alert(info[0].firstChild.data);
document.login.username.value = "";
document.login.password.value = "";
}catch(e){
}
}else{
window.alert("请求错误");
}
}
}

function userCheck(){
var username = document.login.username.value;
var password = document.login.password.value;

if(username ==""){
window.alert("用户名不能为空");
document.login.username.focus();
document.login.password.value = "";
return false;
}else{

sendRequest('login.do?username=' + username + '&password=' + password);
}
}

function test(){
window.alert("test");
}
</script>

<body>
<form action="" method="post" name="login">
<p>
用户名:<input type="text" size="15" name="username"/>
</p>
<p>
密码:<input type="password" size="15" name="password"/>
</p>
<input type="button" value="登录" onclick="userCheck()"/>
</form>
</body>
</html>

"LoginAction.java"
import java.io.IOException;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import java.io.PrintWriter;

public class LoginAction extends Action{
public ActionForward execute(
ActionMapping map,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException{
String username = request.getParameter("username");
String password = request.getParameter("password");

PrintWriter out = response.getWriter();

response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml;charset=UTF-8");
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<response>");
if(username.equals("zhaolinisy") && password.equals("lovesy1117")){
out.println("<res>" + "欢迎您" + "</res>");
}else{
out.println("<res>" + "用户名或密码错误" + "</res>");
}
out.println("</response>");

out.close();

return null;
}
}

很苦恼,靠你们了~谢谢哈
...全文
734 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
adandelion 2012-07-14
  • 打赏
  • 举报
回复
这里发言,表示您接受了CSDN社区的用户行为准则。
ruguosmen 2009-01-14
  • 打赏
  • 举报
回复
怎么不说说怎么解决的那?
sjx5201314 2008-12-17
  • 打赏
  • 举报
回复
我终于解决了Ajax乱码问题。
sjx5201314 2008-12-17
  • 打赏
  • 举报
回复
我也一样啊,疯了
找了很多办法都没解决。
xiaoliaolcy 2007-11-28
  • 打赏
  • 举报
回复
师兄 你这个问题解决没有 我也遇到这样的问题 都要疯了~~~
myjavaoracle 2007-10-05
  • 打赏
  • 举报
回复
顶下5楼,若要使用GET方式,你可以试试修改tomcat的server.xml文件,
在Connector标记修改useBodyEncodingForURI为true。
SOULisy 2007-10-05
  • 打赏
  • 举报
回复
不行了,受不了了,想起他办法,谢谢各位前辈~散分,结帖
SOULisy 2007-10-05
  • 打赏
  • 举报
回复
………………疯了,都试了,还是不行,谁能救救我啊啊啊啊啊啊啊啊啊
myjavaoracle 2007-10-05
  • 打赏
  • 举报
回复
添加属性userBodyEncodingForURI,值为true,再在LoginAction里
request.setCharacterEncoding("UTF-8");
用post的话,在js里加一句XMLHttpReq.setRequestHeader("ContentType",
"application/x-www-form-urlencoded");使得post的数据经过基于UTF-8的URLencode,
你说改成post后还是乱码,是不是少加了这一句?
SOULisy 2007-10-05
  • 打赏
  • 举报
回复
晕了~越说越迷茫了,到底应该用GET,还是用POST??那个比较方便???都应该怎么改啊前辈们??

不管是用GET还是用POST都不行,除了XMLHttpReq.open("GET", url, true);不一样,其他地方需要改吗?相当苦恼啊……

PS:tomcat的server.xml文件中,我怎么找不到userBodyEncodingForURI 这个属性???
SOULisy 2007-10-04
  • 打赏
  • 举报
回复
请问前辈 request.setCharacterEncoding("UTF-9")要加在什么地方?
SOULisy 2007-10-04
  • 打赏
  • 举报
回复
谢谢几位前辈指点,可是XMLHttpReq.open("GET", url, true) 改为 XMLHttpReq("POST", url, true)以后还是不行啊,还是乱码,我就改了这一个地方,其他地方还用改什么吗?
yz394777014 2007-10-04
  • 打赏
  • 举报
回复
你要用GET请求,用POST请求是有中文就会报错,但是可以有解决的办法,,
你要想知道等你试完GET请求有问题在说啊...
...那可是用.net做的哦...
超喜欢的哦...
yaray 2007-10-03
  • 打赏
  • 举报
回复

response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml;charset=UTF-8");
out.println(" <?xml version=\"1.0\" encoding=\"UTF-8\"? >");
都改成GBK呢?
xiangbo520 2007-10-03
  • 打赏
  • 举报
回复
你在程序中使用GET方式传递参数,而TOCMAT中默认的GET方式的编码是ISO8859-1,所以你需要对接收到的编码进行转换,或者改用POST方式再使用request.setCharacterEncoding("UTF-8")在页面设置一下编码就可以了,不过request.setCharacterEncoding("UTF-8")默认情况下在TOMCAT中只对POST方式有效,在RESIN中对POST,GET都有效,主要是因为TOMCAT对POST和GET的处理方式不同造成的。
chenyan1984 2007-10-03
  • 打赏
  • 举报
回复
对于返回XML的用xmlhttp.responseXml

response.setCharacterEncoding("gb2312");

XMLHttpRequest默认是UTF-8,要么修改你的XML文件,要么修改你的XMLHttp对象

使两者使用同一种编码
myjavaoracle 2007-10-03
  • 打赏
  • 举报
回复
含有中文的好象要在服务器端进行escape编码,在客户端unescape解码
输出改成英文就没错,换GBK也同样会乱码。
南南北北 2007-10-03
  • 打赏
  • 举报
回复
XMLHttpReq.open("GET", url, true);
改成:
XMLHttpReq.open("POST", url, true);

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧