社区
Java SE
帖子详情
ajax 利用Servlet向html传值中文问题
稽姬
2006-08-13 10:16:38
用servlet PrintWriter 一个xml 里面有中文 ajax 的xmlhttprequest.responseXML 读取xml文件信息是js显示的中文为乱码,请问有没有什么办法可以解决。直接点就是java类给js传值,中文乱码问题。多谢各位。虽然可以把它们都转成%12%AB可以解决,但是有些麻烦,衷心求教,多谢……
...全文
731
17
打赏
收藏
ajax 利用Servlet向html传值中文问题
用servlet PrintWriter 一个xml 里面有中文 ajax 的xmlhttprequest.responseXML 读取xml文件信息是js显示的中文为乱码,请问有没有什么办法可以解决。直接点就是java类给js传值,中文乱码问题。多谢各位。虽然可以把它们都转成%12%AB可以解决,但是有些麻烦,衷心求教,多谢……
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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里预置中文,也没意义的。
ajax
与
Servlet
,
传值
Demo
ajax
与
Servlet
,
传值
Demo,包含
ajax
传值
乱码转换
province_city.zip
省市联动,前端是JSP,用到了JQuery,后台采用
servlet
来构建。 省市联动,前端是JSP,用到了JQuery,后台采用
servlet
来构建。
jquery实现页面之间的
传值
功能
自己写的一段jquery小代码,实现了jquery的页面
传值
功能,代码已经编译运行通过,请各位大侠指教!
浅谈JSP与
Servlet
传值
及对比(总结)
下面小编就为大家带来一篇浅谈JSP与
Servlet
传值
及对比(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
jsp 复选框
传值
本压缩包有两个页面和一个jquery库,达到的效果是在页面将checkbox以数组的形式 传递,传递之后用jquery遍历,并比较,最后选中。
Java SE
62,630
社区成员
307,264
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章