AJAX乱码问题

 发送数据的时候,如果是以GET方式发送,则对数据进行两次编码:

  var url="<%=basePath%>testAJAX.do?method=testAJAX&testString=" + showInfo;
  url = encodeURI(url);
  url = encodeURI(url);

在后台的SERVLET里面,用
  try {
   testString = java.net.URLDecoder.decode(testString, "utf-8");
  } catch (UnsupportedEncodingException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }

取出数据

写入时,要先写好:
  response.setCharacterEncoding("utf-8");

 

 

整个例子如下:

testAJAX.jsp-----------

<%@ page language="java" contentType="text/html;charSet=utf-8" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/tags/struts-html" prefix="html"%>
<%@taglib uri="/tags/struts-bean" prefix="bean"%>
<%@taglib uri="/tags/struts-logic" prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'testAjax.jsp' starting page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <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">
 -->
<script language="javascript">
    //ajax函数调用
    var xmlHttp;
    function createXMLHttpRequest()
    {
    if(window.ActiveXObject)
    {
     xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest)
    {
    xmlHttp=new XMLHttpRequest();
    }
 }
 //回调函数
 function handleCallback()
 {
  if(xmlHttp.readyState==4)
  {
   if(xmlHttp.status==200)
   {
    parseResult();
   }
   }
 }
 //显示信息
 function parseResult(){
        var response=xmlHttp.responseText;
  document.getElementById("showStr").innerHTML = response;
 }
 //主函数
 function showMess(){
  createXMLHttpRequest();
  var showInfo = form1.testString.value;
  alert(showInfo);
  var url="<%=basePath%>testAJAX.do?method=testAJAX&testString=" + showInfo;
  url = encodeURI(url);
  url = encodeURI(url);
  xmlHttp.open("GET",url,true);
  xmlHttp.onReadyStatechange=handleCallback;
  xmlHttp.send(null);/*
  url = encodeURI(url);
  xmlHttp.setRequestHeader("Content-Type","text/html;charset=UTF-8");
  xmlHttp.setRequestHeader("If-Modified-Since","0"); */
 }
 //主函数
 function showMessC(){
  createXMLHttpRequest();
  var showInfo = form1.testString.value;
  var cStr = "testString=" + showInfo;
  var url="<%=basePath%>testAJAX.do?method=testAJAX";
  xmlHttp.open("post",url,true);
  xmlHttp.onReadyStatechange=handleCallbackC;
  xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
  xmlHttp.send(cStr);/*
  xmlHttp.setRequestHeader("If-Modified-Since","0"); */
 }
 //回调函数
 function handleCallbackC()
 {
  if(xmlHttp.readyState==4)
  {
   if(xmlHttp.status==200)
   {
    parseResultC();
   }
   }
 }
 //显示信息
 function parseResultC(){
        var response=xmlHttp.responseText;
  document.getElementById("showStrC").innerHTML = response;
 }
</script>
  </head>
 
  <body>
    <form name="form1" action="/testAjax.do">
     <table><tr>
     <td>testString: </td><td><input type="text" name="testString" ></td>
     <td>showString: </td><td><input type="text" name="TS" ></td>
     </tr></table>
     <input type="button" value="test" οnclick="showMess()">: <pre id="showStr">infor here...</pre><br>
     <input type="button" value="test" οnclick="showMessC()">: <pre id="showStrC">infor here...</pre>
    </form>
  </body>
</html>

 

TestAJAXAction-------------

package com.test.action.L;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.test.tools.L.Tools;

public class TestAJAXAction  extends DispatchAction{
 @SuppressWarnings({ "finally", "finally" })
 public ActionForward testAJAX(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response){
  String testString = request.getParameter("testString");
  try {
   testString = java.net.URLDecoder.decode(testString, "utf-8");
  } catch (UnsupportedEncodingException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }

  String methodStr = request.getMethod();
  response.setCharacterEncoding("utf-8");  StringBuffer show = new StringBuffer();
  PrintWriter pw=null;

  try{
   pw = response.getWriter();
   

   
   show.append("<font color='red'>");
   show.append("the order is: ");
   show.append(testString);
   show.append("</font>");
   
   pw.write(show.toString());
   pw.flush();
  }catch(Exception e){
   e.printStackTrace();
   System.out.println("打开输出流出错了"+e.getMessage());
  }finally{
   pw.close();
   return null;
  }
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值