当前位置: 代码迷 >> Web前端 >> url 传汉字步骤
  详细解决方案

url 传汉字步骤

热度:145   发布时间:2013-10-31 12:03:52.0
url 传汉字方法

js中传递中文参数到服务器端乱码问题?

1.js中传递中文参数到后台时出现乱码问题的解决方法是什么呢??

答:例如js中写法如下:?

????? $.get("http://localhost:8080/UserVertify?userName="+userName,null,function(response){?
??????????? $("#result").html(response);?
??????? });?

????? 这是使用jquery进行ajax操作时在js中写的服务器端请求,参数中userName要是中文的话在服务器端接收的时候就会是乱码。?

???? 我们的解决办法是:?

???? $.get("http://localhost:8080/UserVertify?userName="+encodeURI(encodeURI(userName)),null,function(response){?
??????????? $("#result").html(response);?
??????? });?

对userName进行双重encodeURI编码。到服务器端之后:?

????? String username = request.getParameter("userName");?
????? username = URLDecoder.decode(username,"UTF-8");?

采用你所需要的编码方式进行解码就可以解决乱码问题了。?

?

?

js中?encodeURI(encodeURI(userName))

?

java中:URLDecoder.decode(username,"UTF-8");?

  相关解决方案