• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

jquery 搞代码 7年前 (2018-06-12) 170次浏览 已收录 0个评论

  这两天在继续我的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″ style=” 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试试看。

欢迎大家阅读《Ajax开发过程中提交获取数据的乱码问题…_jquery,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Ajax开发过程中提交获取数据的乱码问题_jquery

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址