天天看点

json传输数据解决中文乱码问题

1.Ajax在url带参数(中文):

   encodeURI(encodeURI(expireDesc))//设置编码

2.后台接收需要转码:

  URLDecoder.decode(expireDesc, "UTF-8") //将接收的参数转码

3.例子:

js Ajax:

function exchange(expireDesc){

            $.ajax({

             type: "post",  

             url:ctx+"/xxx.do?method=xxx&xxx="+  encodeURI(encodeURI(xxx)),

             dataType: "json",

             success: function(data){

                 xxxx=data.code;

             }

            })

        }

后台接收:

public JsonResult exchange(HttpServletRequest request,String expireDesc) {

                String expire="";

                try {

                     xxx = URLDecoder.decode(xxx, "UTF-8");//将接收的参数转码,用于解决中文乱码

                } catch (UnsupportedEncodingException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }

            MngCodeEntity list = produceService.exchange(expire);

            JsonResult jsonResult = new JsonResult();

            jsonResult.setCode(list.getCodeVal());

            return jsonResult;

        }

转载于:https://www.cnblogs.com/xiaowenzi/p/6857738.html