当前位置: 代码迷 >> Java Web开发 >> ajax提交正常,form提交却乱码咋回事
  详细解决方案

ajax提交正常,form提交却乱码咋回事

热度:328   发布时间:2016-04-13 22:45:24.0
ajax提交正常,form提交却乱码怎么回事?
我写了个简单的页面如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="/WEB-INF/tld/c.tld"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@include file="/WEB-INF/jsp/common/commonPage.jsp" %>
<html>
  <head>
    <title>全站搜索</title>
    
<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="全站搜索">

<!-- 
<link href="<%=basePath%>resources/cmcc/css/default.css" rel="stylesheet" type="text/css" />
<script src="<%=basePath%>resources/cmcc/js/cloud-zoom.1.0.2.js" language="javascript" type="text/javascript"/>
    -->
    
    <script type="text/javascript">
    function doSearch(){
    
    $.ajaxRequest({
        type : "post",
        url : "<%=basePath%>operate/globalSearch!totalSearch.action",
        data : {"category":$("#category").val(),"search_words":$("#search_words").val()},
        dataType : "json",
        async : false,
        success : function(data) {
        
        }
        });
    }
    </script>
    
    
  </head>

  <body>
  
    <center>
      <div>搜索首页</div>
    </center>
    
    <center>
    <form action="<%=basePath%>operate/globalSearch!totalSearch.action">
    <s:select list="categoryList"  listValue="name" listKey="value"  name="category" id="category"></s:select>  
    <input id ="search_words" name="search_words"/>
    <input type="button" id="search" value="搜索一下" onclick="doSearch()"></input>
</form>
 </center>  
  
  </body>
</html>


按钮改成submit提交,到了后台一跟,输入框里的中文成了乱码,改成如上用ajax提交却正常,这是怎么回事,页面编码为utf-8
------解决思路----------------------
form表单标签里加个属性 method="post"
------解决思路----------------------
你ajax用的post,form用的get, request.setCharacter()貌似对get无效
------解决思路----------------------
这两种方式提交的区别就是,ajax用的post提交,而你的from表单默认是get方式提交。
后台对于get提交,好像请求设置字符集有问题。
直接在你的from表单使用method="post"的方式来提交吧。
如果不想用post.那就,这样去转吧new String(xxx.getBytes("iso8859-1"),"utf-8")。
最好的方式,还是写个过滤器,来对所有请求进行字符编码过滤。
  相关解决方案