Ajax开发过程中提交获取数据的乱码问题

80酷酷网    80kuku.com

  ajax|过程|数据|问题

  这两天在继续我的Ajax之旅。一直在抓紧步伐。

  前天试验使用Ajax提交form表单到服务器,获取form信息内容出现了乱码,一时无法解决。网上有结论说是application/x-www-form-urlencoded编码的原因,我估计也是。正在寻找解决之道。

  大家一起讨论讨论。

  补充:通过在后台用UTF8转码的方式可以解决中文乱码问题。这种方法针对form表单提交,编码在服务器完成。代码如下:

  form.jsp: 

<% page contentType="text/html; charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>表单提交测试</title>
<script language="javascript">
 var http_request = false;
 function send_request(url,poststr) {//初始化、指定处理函数、发送请求的函数
  http_request = false;
  //开始初始化XMLHttpRequest对象
  if(window.XMLHttpRequest) { //Mozilla 浏览器
   http_request = new XMLHttpRequest();
   if (http_request.overrideMimeType) {//设置MiME类别
    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) { // 异常,创建对象实例失败
   window.alert("不能创建XMLHttpRequest对象实例.");
   return false;
  }
  http_request.onreadystatechange = processRequest;
  // 确定发送请求的方式和URL以及是否同步执行下段代码
  http_request.open("POST", url, true);
  http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  http_request.send(poststr);
 }
 // 处理返回信息的函数
    function processRequest() {
        if (http_request.readyState == 4) { // 判断对象状态
            if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
                alert(http_request.responseText);
            } else { //页面不正常
                alert("您所请求的页面有异常。");
            }
        }
    }
 function form_submit() {
  var f = document.form1;
  var username = f.username.value;
  var password = f.password.value;
  var poststr = "";
  if(username=="") {
   window.alert("用户名不能为空。");
   f.username.focus();
   return false;
  }
  if(password=="") {
   window.alert("密码不能为空。");
   f.password.focus();
   return false;
  }
  poststr = "username="+encodeURI(f.username.value)+"&password="+f.password.value;
  send_request('form_handle.jsp',poststr);
  return false;
 }
</script>
</head>

<body>
<form name="form1" method="post" action="" >
    <table width="300" border="0" cellspacing="4" cellpadding="0" font-size:12pt;">
        <tr>
            <td width="74" height="25">用户名:</td>
            <td width="220" height="25"><input name="username" type="text" id="username" size="20"></td>
        </tr>
        <tr>
            <td height="25">密码:</td>
            <td height="25"><input name="password" type="password" id="password" size="20"></td>
        </tr>
        <tr align="center">
            <td height="25" colspan="2"><input type="submit" name="Submit" value="提交"></td>
        </tr>
    </table>
</form>
</body>
</html>

  form_handle.jsp:

<% page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%
String username = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF8");
String password = request.getParameter("password");
System.out.println("用户名:"+username);
System.out.println("密码:"+password);
out.println(username+"|"+password);
%>

  另外,有些网友反应生成包含中文内容的XML文档返回到客户端也会出现乱码问题。建议将XML文档的编码方式改成UTF8试试看。



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间

上一篇WAP中级教程

下一篇AJAX实例入门

点击: