问题很简单:就是ajax返回的中文是乱码,用的weblogic,直接看代码吧,在线等,解决了立马给分jsp页面:
<%@ page contentType="text/html; charset=gbk"%>
js:function runAjax(url){
var str;
if (window.XMLHttpRequest) {
       req = new XMLHttpRequest(); 
   }else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP"); 
   }
   if(req){
req.open("POST",url, false); 
req.onreadystatechange =  function(){
   if (req.readyState == 4) {
   if (req.status == 200) {
  str = req.responseText;
  }
   }
}; 
req.send(); 
   }
  if(str == null)
  str = "";
  return str.trim();
}
action:
public void getNsrxx(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
try {
String swdjh = request.getParameter("swdjh");
System.out.println("swdjh=="+swdjh);
NsrPo nsr = ggUtil.getNsrxxBySh(swdjh);
StringBuffer re = new StringBuffer();
if (nsr != null) {
re.append(nsr.getNsrMc()).append("&");
re.append(nsr.getFwsDm()).append("&");
re.append(nsr.getDh()).append("&");
re.append(nsr.getDz()).append("&");
} else {
re.append("false");
}
System.out.println("re=="+re);
response.getWriter().println(re);
} catch (Exception e) {
e.printStackTrace();
}
}过滤器:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpServletResponse httpResponse = (HttpServletResponse)response;
if (httpRequest.getCharacterEncoding() == null) {
httpRequest.setCharacterEncoding("gbk");
}
if (httpResponse.getCharacterEncoding() == null) {
httpResponse.setCharacterEncoding("gbk");
httpResponse.setContentType("text/html");
}
chain.doFilter(request, response); }

解决方案 »

  1.   

    不知道为什么 一设为utf-8就报错。。
      

  2.   

    是不是你的过滤器没截住ajax?
      

  3.   

      public void getNsrxx(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
            try {
                String swdjh = request.getParameter("swdjh");
                System.out.println("swdjh=="+swdjh);
                NsrPo nsr = ggUtil.getNsrxxBySh(swdjh);
                StringBuffer re = new StringBuffer();
                if (nsr != null) {
                    re.append(nsr.getNsrMc()).append("&");
                    re.append(nsr.getFwsDm()).append("&");
                    re.append(nsr.getDh()).append("&");
                    re.append(nsr.getDz()).append("&");
                } else {
                    re.append("false");
                }
                System.out.println("re=="+re);
                response.getWriter().println(re);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    你把re看中文是不是乱码
    这个要不用个STRING的转码吧 response.getWriter().println(re);
    这个re 不需要转成String 吗
      

  4.   

    post方式提交请求的话,还要设置header值吧!
      

  5.   


    设置成UTF-8也会报错? 什么错
      

  6.   

    编码问题吧  设置成utf-8之后,在MyEclipse项目里也要设置成utf-8的编码方式
      

  7.   

    后台打印的都是正确的,上传的参数是中文也可以,就是用ajax接收到的时候是乱码了,我比较急,能不能直接给代码我直接试。。
      

  8.   

    System.out.println("re=="+re);
    String str=re.toString();
    str=new String(str.getBytes("iso8859-1"),"utf-8");
    System.out.println(str);你试试吧    希望可以帮你
      

  9.   

    req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    写在send之前