论坛首页 Web前端技术论坛

关于《[原创]一个ajax的例子》的中文问题

浏览 11373 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-12-07  
刚开始接触ajax 搞了一个例子在看  但是发现中文是乱码(代码在2楼)

在 ("<city>hangzhou</city><city>huzhou</city>")  这里换成中文的话 页面显示就是乱码 搞不

清楚是怎么回事  请大家看看
   发表时间:2005-12-07  
html文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>MyHtml.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

</head>
<script type="text/javascript">
function getResult(stateVal) {
        var url = "servlet/SelectCityServlet?state="+stateVal;
        if (window.XMLHttpRequest) {
                req = new XMLHttpRequest();
        }else if (window.ActiveXObject) {
                req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if(req){
                req.open("GET",url, true);
                req.onreadystatechange = complete;
                req.send(null);
        }
}
function complete(){
        if (req.readyState == 4) {
                if (req.status == 200) {
                        var city = req.responseXML.getElementsByTagName("city");
                        alert(city.length);
                        var str=new Array();
                        for(var i=0;i<city.length;i++){
                                str[i]=city[i].firstChild.data;
                        }
                        alert(document.getElementById("city"));
                        buildSelect(str,document.getElementById("city"));
                }
        }
}
function buildSelect(str,sel) {
        sel.options.length=0;
        for(var i=0;i<str.length;i++) {
                sel.options[sel.options.length]=new Option(str[i],str[i])
        }
}
function test(){
        alert("test");
}
</script>
<body>
<select name="state" onChange="getResult(this.value)">
        <option value="">请选择</option>>
        <option value="zj">浙江</option>>
        <option value="zs">江苏</option>>
</select>
<select id="city"></select>
</body>
</html>


Servlet 类:
package com.stephen.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author stephen
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class SelectCityServlet extends HttpServlet {


        public SelectCityServlet() {
                super();
        }

        public void destroy() {
                super.destroy();
        }

        public void doGet(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException {
                response.setContentType("text/xml");
                response.setHeader("Cache-Control", "no-cache");
                String state = request.getParameter("state");
                StringBuffer sb=new StringBuffer("<state>");
                if ("zj".equals(state)){
                        sb.append("<city>hangzhou</city><city>huzhou</city>");
                } else if("zs".equals(state)){
                        sb.append("<city>nanjing</city><city>yangzhou</city><city>suzhou</city>");
                }
                sb.append("</state>");
                PrintWriter out=response.getWriter();
                out.write(sb.toString());
                out.close();
        }

}

web.xml文件:
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>
<servlet-name>SelectCityServlet</servlet-name>
<servlet-class>com.stephen.servlet.SelectCityServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>SelectCityServlet</servlet-name>
<url-pattern>/servlet/SelectCityServlet</url-pattern>
</servlet-mapping>

</web-app>
0 请登录后投票
   发表时间:2005-12-07  
在html文件里写明gbk看看那
0 请登录后投票
   发表时间:2005-12-08  
构照 xml 返回数据,最好不要用StringBuffer 的方式。

但如果是这样的话,

response.setContentType("text/xml");  这句要添加编码信息。

同时构照的xml 加上xml头 <?xml version="1.0" encoding="gb2312"?>
0 请登录后投票
   发表时间:2005-12-08  
都不行了!
为什么是这个结果呢?
0 请登录后投票
   发表时间:2005-12-12  
看看你的几个文件的编码是不是GB2312。
0 请登录后投票
   发表时间:2005-12-12  
responseText和responseXML只支持utf8
0 请登录后投票
   发表时间:2005-12-19  
http://www.hxinze.com/ajax/_city/

我这个就是GB2312的,,就是不乱码
0 请登录后投票
   发表时间:2005-12-19  
用utf-8吧,使用gb2312指不定IE又出什么问题
response.setContentType("text/xml;charset=gb2312");
0 请登录后投票
   发表时间:2005-12-22  
为什么出现的是问号呢????

到底是不是乱码呢
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics