ajax get方法乱码

xuexijava 2010-01-29 04:18:32
HTML代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ajaxTest1.html</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}

function createQueryString(){
var firstName=document.getElementById("firstName").value;
var lastName=document.getElementById("lastName").value;
var bb=document.getElementById("bb").value;
var queryString="firstName="+firstName+"&lastName="+lastName+"&bb="+bb;

return queryString;
}

function doRequestUsingGet(){
createXMLHttpRequest();
var queryString="testPostAndGet.do?";
queryString=queryString+createQueryString()+"×tamp="+new Date().getTime();
alert(queryString);
xmlHttp.onreadystatechange=callBack;
xmlHttp.open("GET",queryString,true);

xmlHttp.send(null);
}
function doRequestUsingPost(){
createXMLHttpRequest();
var url="testPostAndGet.do?timeStamp="+new Date().getTime();
var queryString=createQueryString();


xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=callBack;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(queryString);

}
function callBack(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
parseResults();
}
}
}
function parseResults(){
var responseDiv=document.getElementById("serverResponse");
if(responseDiv.hasChildNodes()){
responseDiv.removeChild(responseDiv.childNodes[0]);

}
var responseText=document.createTextNode(xmlHttp.responseText);
responseDiv.appendChild(responseText);


}
</script>

</head>

<body>
<table>
<tbody>
<tr>
<td>姓:</td><td><input type="text" id="firstName"/></td>
</tr>
<tr>
<td>名:</td><td><input type="text" id="lastName"/></td>
</tr>
<tr>
<td>生日:</td><td><input type="text" id="bb"/></td>
</tr>
</tbody>
</table>
<div>
<form action="#" name="form1">
<input type="button" value="以GET发送数据" onclick="doRequestUsingGet()"/>
<input type="button" value="以POST发送数据" onclick="doRequestUsingPost()"/>
</form>
</div>
<h2>服务器返回结果:</h2>
<div id="serverResponse"></div>
</body>
</html>
服务器端代码:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// response.setHeader("Charset","UTF-8");
// response.setHeader("Charset","GB2312");
response.setContentType("text/xml;charset=UTF-8");
String firstName=request.getParameter("firstName");
System.out.println("firstName:"+firstName);
String lastName=request.getParameter("lastName");
String bb=request.getParameter("bb");
System.out.println("is action ok");

String responseText="hello"+firstName+lastName+"你的生日是:"+bb+"你发送数据所使用的方法是:"+request.getMethod();
System.out.println(responseText);
try{
PrintWriter out=response.getWriter();
out.println(responseText);
// out.write(responseText);
out.close();
System.out.println("is action end");
}catch(Exception e){

e.printStackTrace();
}
return null;
}
环境Struts1.3用了过滤器UTF-8编码,Java代码和Html都用的UTF-8,以Post方式发送数据正常显示不出现乱码
但是以GET方式发送乱码
何解?
哪位达人给解释下。。。。。
到底是为什么呢?
...全文
845 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuexijava 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 sotom 的回复:]
前端jsp
encodeURI(encodeURI("xxxx"));

Servlet

URLDecoder.decode(request.getxxxx,"UTF-8");
[/Quote]
好使。。。
sotom 2010-01-30
  • 打赏
  • 举报
回复
前端jsp
encodeURI(encodeURI("xxxx"));

Servlet

URLDecoder.decode(request.getxxxx,"UTF-8");
licip 2010-01-30
  • 打赏
  • 举报
回复
去tomcat的配置文件server.xml中,把<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />加一个属性:URIEncoding="UTF-8"问题就可以解决了。
cbajing 2010-01-30
  • 打赏
  • 举报
回复
帮顶
简单007 2010-01-30
  • 打赏
  • 举报
回复
dd
xuexijava 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 linshenxiu 的回复:]
用get方式提交过滤器是不管用的,可以在接收的时候进行转码:
String para=new String(request.getParameter("参数名").getBytes("iso-8859-1"),"gbk");
[/Quote]
Test了下,还是乱码啊。。。
?那Post过滤器有用吗?
还是AJAX的异步请示都不经过过滤器?
等待高人指点。。。。。
linshenxiu 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 linshenxiu 的回复:]
用get方式提交过滤器是不管用的,可以在接收参数的时候进行转码:
String para=new String(request.getParameter("参数名").getBytes("iso-8859-1"),"gbk");
[/Quote]
试试这个方法,我试过,没错的。
xuexijava 2010-01-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xblue3 的回复:]
escape()
[/Quote]
3Q给点代码看吗?
想知道为什么乱码
xuexijava 2010-01-29
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zhang116868 的回复:]
用encodeURI() 方法吧!

去这里看看吧,有详细介绍:
http://do.jhost.cn/sunshine/ReadNews?action=read&id=54
[/Quote]
打不开。。。
xuexijava 2010-01-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ibm_hoojo 的回复:]
get方法传文字字符串就会有乱码,因为是通过url传参的。
所以你要在js客户端经过2次转码,同样服务器端也要转码。
$.get("AjaxService?userName=" + encodeURI(encodeURI(userName)), null, function (data) {
$("#result").html(data);
});
这2个效果是一样的:encodeURIComponent(userName) 、 encodeURI(encodeURI(userName))
服务器端转码:String userName = URLDecoder.decode(request.getParameter("userName"), "UTF-8");
[/Quote]
先谢谢你的回答。。
按你说的方式改过,乱码依旧。。。。。。。
linshenxiu 2010-01-29
  • 打赏
  • 举报
回复
用get方式提交过滤器是不管用的,可以在接收的时候进行转码:
String para=new String(request.getParameter("参数名").getBytes("iso-8859-1"),"gbk");
tuo_bing 2010-01-29
  • 打赏
  • 举报
回复
mark ,学习 。。。
海会圣贤 2010-01-29
  • 打赏
  • 举报
回复
用encodeURI() 方法吧!

去这里看看吧,有详细介绍:
http://do.jhost.cn/sunshine/ReadNews?action=read&id=54
meadking 2010-01-29
  • 打赏
  • 举报
回复
escape()
luffyke 2010-01-29
  • 打赏
  • 举报
回复
lstar66 2010-01-29
  • 打赏
  • 举报
回复
GET方式的请求是没有请求体(body)的,放在URL后面,所有参数都将附加到url后传递给服务器
而POST放到请求体(body)中
传参数的话就就POST吧
luffyke 2010-01-29
  • 打赏
  • 举报
回复
request.setCharacterEncoding("UTF-8");
hoojo 2010-01-29
  • 打赏
  • 举报
回复
get方法传文字字符串就会有乱码,因为是通过url传参的。
所以你要在js客户端经过2次转码,同样服务器端也要转码。
$.get("AjaxService?userName=" + encodeURI(encodeURI(userName)), null, function (data) {
$("#result").html(data);
});
这2个效果是一样的:encodeURIComponent(userName) 、 encodeURI(encodeURI(userName))
服务器端转码:String userName = URLDecoder.decode(request.getParameter("userName"), "UTF-8");
xuexijava 2010-01-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yaku2688 的回复:]
给你顶起来
[/Quote]
3Q
yaku2688 2010-01-29
  • 打赏
  • 举报
回复
给你顶起来

52,792

社区成员

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

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