前台提交数据到后台为乱码问题解决方式

1、先看浏览器编码是否ok;

2、修改请求为post;(get提交,中文容易乱码)

3、接口指定编码utf-8;(指定content-type)

我的问题是将get改为 post 解决的;

也可参考其他resource,such as:

=================================================================

转自:https://www.cnblogs.com/sxdcgaq8080/p/5741461.html;

【前台 乱码】 前台单独乱码+后台往前台传输的数据乱码

解决方法:

第一:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> 

  这句话是加在每个页面的最上面,在<!DOCTYPE html>之前加的
  并且
  request.setCharacterEncoding("gb2312");

第二:

 配置Tomcat的 server.xml。

  <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
  <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"  URIEncoding="UTF-8"/>

 

第三:

配置 web.xml  
     <filter>
      <filter-name>setEncoding</filter-name>
      <filter-class>com.dinner.filter.CharsetFilters</filter-class>
      <init-param>
         <param-name>coding</param-name>
         <param-value>UTF-8</param-value>
      </init-param>
     </filter>
     <filter-mapping>
        <filter-name>setEncoding</filter-name>
        <url-pattern>/*</url-pattern>
     </filter-mapping>

第四:

 工程编码
     开发中发现Windows版Eclipse默认Java和Web工程的默认编码方式不那么遂人愿,修改方法如下:
     1. Web工程文件编码修改方式
       Window -> Preference -> Content types
       推荐将Web相关文件的编码都设置为UTF-8
     2. Java源文件编码修改方式
       Window -> Preference -> Workspace -> Text file encoding
       Windows平台默认为GBK,Linux平台默认为UTF-8

第五:

  如果使用ajax前后台数据交互,可以改变传输方式,将type设置为post

$.ajax({url:"productAdd.htmls",
                        type:"post",
                        data:{
                            "productName" : productName,
                            "productCre" : productCre
                        },
                        success:function(data){
                            if(data != null){
                                var product = eval("("+data+")");
                                temp="<tr class='text-c'><td><input type='checkbox' value='"+product.productId+
                                "'></td><td>"+product.productName+"</td><td><a href='checkdisease.htmls'>【查看疾病信息】</a><a href='updateproduct.htmls'>【更新】</a></td></tr>";
                                $('.table-bordered tbody tr:first' , parent.document).before(temp);
                                $('.table-bordered tbody tr:last' , parent.document).remove();
                            }    
                            // var index = parent.layer.getFrameIndex(window.name); 可以用这个获取当前要关闭的layer ,也可以使用parent.indexProductAdd 获取在父层定义的那个layer。open()的弹窗
                            parent.layer.close(parent.indexProductAdd); //获取到layer的弹出窗 关闭它  
                }});
复制代码

 

这样即可以解决!!!

第六:

在type不能设置为post的情况下,可以在后台接收到数据之后,自行进行解码

【 URLDecoder.decode(condition, "utf-8");】参数1:字符串    参数2:编码方式

复制代码
@RequestMapping(value= "/statistics" ,produces = "text/html;charset=UTF-8")
    @ResponseBody 
    public String statistics(HttpServletRequest request,String condition,String  questOptions) throws UnsupportedEncodingException{
        questOptions = questOptions.replaceAll("category=", "");
        String [] questArr = questOptions.split("&");
        
        condition = URLDecoder.decode(condition, "utf-8");
        System.out.println(condition);
        System.out.println(questOptions);
        return null;
    }
复制代码

在这里 获取到转码后的字符串的 效果 比【new String(str.getBytes("ISO-8859-1"),"utf-8")】要好得多!

第七:

  整个项目,单页面跳转的情况下,出现乱码问题怎么解决?

跳转到:http://www.cnblogs.com/sxdcgaq8080/p/8085680.html

 

转载于:https://www.cnblogs.com/xh_Blog/p/8809021.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值