ajax 利用Servlet向html传值中文问题

稽姬 2006-08-13 10:16:38
用servlet PrintWriter 一个xml 里面有中文 ajax 的xmlhttprequest.responseXML 读取xml文件信息是js显示的中文为乱码,请问有没有什么办法可以解决。直接点就是java类给js传值,中文乱码问题。多谢各位。虽然可以把它们都转成%12%AB可以解决,但是有些麻烦,衷心求教,多谢……
...全文
719 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinkerwu 2006-08-24
  • 打赏
  • 举报
回复
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>rq.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<script language="javascript" type="text/javascript">
var xmlDoc;
var http_request=false;

function initXMLHttp() {
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert("您需要升级您的浏览器!");
return false;
}
}

function yourfunction(){
initXMLHttp();
http_request.open("get", "http://localhost:7001/demo/jugpersonSelectedAction.do?d=1", false);
http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
http_request.send(null);
xmlDoc=http_request.responseXML;
alert(xmlDoc);
var data=xmlDoc.getElementsByTagName("node1");
document.getElementById("div1").innerHTML=data[0].firstChild.nodeValue;
}
</script>
</head>

<body>
<input type="button" onclick="yourfunction()" name="button" value="Click me">
<div id="div1"></div>
</body>
</html>

我把rs2.jsp换成servlet为什么取不到返回值哦。。
大家帮忙。。。

public class jugpersonSelectedAction extends Action
{
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
String forward = "success";
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-cache");
response.setHeader("expires","0");
response.setHeader("content-type","text/xml;charset=gb2312");
System.out.println("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
System.out.println("<docroot><node1>中国</node1></docroot>");

return actionMapping.findForward(forward);

}
jimshen 2006-08-16
  • 打赏
  • 举报
回复
第二个文件改一下,IE和FF都可以了

<%@ page language="java" pageEncoding="gb2312"%><%
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-cache");
response.setHeader("expires","0");
response.setHeader("content-type","text/xml;charset=gb2312");
out.println("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
out.println("<docroot><node1>中国</node1></docroot>");
%>
稽姬 2006-08-16
  • 打赏
  • 举报
回复
我用servlet写的 response xml就是乱码 嗷嗷郁闷,结贴,继续研究中……
jimshen 2006-08-15
  • 打赏
  • 举报
回复
怎么会不行呢?下面是在FireFox中刚刚通过的

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>rq.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<script language="javascript" type="text/javascript">
var xmlDoc;
var http_request=false;

function initXMLHttp() {
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert("您需要升级您的浏览器!");
return false;
}
}

function yourfunction(){
initXMLHttp();
http_request.open("get", "rs2.jsp", false);
http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
http_request.send(null);
xmlDoc=http_request.responseXML;
var data=xmlDoc.getElementsByTagName("node1");
document.getElementById("div1").innerHTML=data[0].firstChild.nodeValue;
}
</script>
</head>

<body>
<input type="button" onclick="yourfunction()" name="button" value="Click me">
<div id="div1"></div>
</body>
</html>


----------------------------------

<%@ page language="java" pageEncoding="gb2312"%>
<%
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-cache");
response.setHeader("expires","0");
response.setHeader("content-type","text/xml;charset=gb2312");
out.println("<docroot><node1>中国</node1></docroot>");
%>







ftiger 2006-08-15
  • 打赏
  • 举报
回复
建议全部utf-8
包括数据库.jsp,htm,js,Servlet,javaBeen就不存在什么转码不转码的问题了。
稽姬 2006-08-15
  • 打赏
  • 举报
回复
自己顶……
OnlyFor_love 2006-08-14
  • 打赏
  • 举报
回复
xmlhttprequest.responseXML接受到的xml都是utf-8格式的 你需要转码
crazycy 2006-08-14
  • 打赏
  • 举报
回复
在页面上增加那个头部文件了么?声明字体的那部分(jsp中是<%@ page contentType="text/html, charset=gb2312"%>);
html中也有类似的声明,找一下,加上后,看能否解决
稽姬 2006-08-14
  • 打赏
  • 举报
回复
多谢楼上几位,这些方法我都用过了。都不管用的,没办法,才来求助,多谢多谢……
1楼的朋友好像没看清楚我问的什么,那个问题时文本编辑器的字符集问题。
issgates 2006-08-14
  • 打赏
  • 举报
回复
response.setContentType("text/xml;charset=UTF-8");
zhmt 2006-08-14
  • 打赏
  • 举报
回复
up!

mark!
rickhunterchen 2006-08-14
  • 打赏
  • 举报
回复
response.setContentType("text/xml;charset=gb2312");
cuiyingfeng 2006-08-14
  • 打赏
  • 举报
回复
你的xml文件编码方式,文件保存的时候选择编码方式和输出编码方式三者要统一。有3种编码方式可参考:UTF-8、gb2312和gbk。
jimshen 2006-08-14
  • 打赏
  • 举报
回复
response.setContentType("text/xml;charset=gb2312");
response.setContentType("text/html;charset=gb2312");
yiyi0518 2006-08-14
  • 打赏
  • 举报
回复
ps: 您的js是二进制文件么??
我碰到过用eclipse编辑中文,页面上显示的是乱码,用ultraEdit编辑 js 就不会有这个问题。
稽姬 2006-08-14
  • 打赏
  • 举报
回复
To:OnlyFor_love
在js中应该怎么转码呢?求教,在baidu上没找到,多谢
稽姬 2006-08-13
  • 打赏
  • 举报
回复
还有如果传递数字或英文做状态值,在js里预置中文,也没意义的。

62,614

社区成员

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

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