锁定老帖子 主题:解决: js向后台传参数出现乱码
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-08-03
var typeselect = form2.typeselect.value; //var typeselect = document.getElementById('typeselect').value; //怎么获得值得自己用自己的方式, 不一定要像我酱紫.. typeselect=encodeURI(typeselect); typeselect=encodeURI(typeselect); form2.action="xxxx.do?typeselect="+typeselect; form2.submit(); 在后台进行获取: import java.net.URLDecoder; if (request.getParameter("typeselect") != null) { typeid = request.getParameter("typeselect"); typeid = URLDecoder.decode(typeid, "utf-8"); } 经测试, 乱码问题得以解决; 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-08-03
js程序块里面使用encodeURI(varname)要使用两次 ..
|
|
返回顶楼 | |
发表时间:2010-08-03
这样做貌似中文字符很长的话,速度是相当的慢滴!!
|
|
返回顶楼 | |
发表时间:2010-08-04
为什么要encode两次?吾是菜鸟,请解释一下嘛
|
|
返回顶楼 | |
发表时间:2010-08-04
smiky 写道 为什么要encode两次?吾是菜鸟,请解释一下嘛
看你后台用的是什么语言咯,如果是asp.net,那前端只需一次 escape ,后端一次解码即可。 如果是PHP, java,那前端需要两次encodeURI,因为一次encodeURI还有一些字符不会被编码。 |
|
返回顶楼 | |
发表时间:2010-08-04
joycepro 写道 smiky 写道 为什么要encode两次?吾是菜鸟,请解释一下嘛
看你后台用的是什么语言咯,如果是asp.net,那前端只需一次 escape ,后端一次解码即可。 如果是PHP, java,那前端需要两次encodeURI,因为一次encodeURI还有一些字符不会被编码。 我刚查到的说法是,对于php用$_get方法来获取数据,它会自动decode一次,所以要encode两次, java也是这样吗? |
|
返回顶楼 | |
发表时间:2010-08-04
恩,有的时候确实需要encode两次,通常都用过滤器encode一下也可以吧
|
|
返回顶楼 | |
发表时间:2010-08-04
smiky 写道 joycepro 写道 smiky 写道 为什么要encode两次?吾是菜鸟,请解释一下嘛
看你后台用的是什么语言咯,如果是asp.net,那前端只需一次 escape ,后端一次解码即可。 如果是PHP, java,那前端需要两次encodeURI,因为一次encodeURI还有一些字符不会被编码。 我刚查到的说法是,对于php用$_get方法来获取数据,它会自动decode一次,所以要encode两次, java也是这样吗? 通常都是使用request.getParameter("paramname")获取参数数据,在request对象得到的数据都是经过解码过的,而解码过程中程序里是无法指定的.有很多新手说用request.setCharacterEncoding("字符集")可以指定解码方式,其实是不可以的,看servlet的官方API说明有对此方法的解释:Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader(). 可以看出: 在doGet()方法中, 这样的方式是无能为力的。 |
|
返回顶楼 | |
发表时间:2010-08-04
shore1111 写道 恩,有的时候确实需要encode两次,通常都用过滤器encode一下也可以吧
写过滤器,设置request.setCharacterEncoding(”UTF-8″), 也是可以的哈. |
|
返回顶楼 | |
发表时间:2010-08-04
joycepro 写道 smiky 写道 joycepro 写道 smiky 写道 为什么要encode两次?吾是菜鸟,请解释一下嘛
看你后台用的是什么语言咯,如果是asp.net,那前端只需一次 escape ,后端一次解码即可。 如果是PHP, java,那前端需要两次encodeURI,因为一次encodeURI还有一些字符不会被编码。 我刚查到的说法是,对于php用$_get方法来获取数据,它会自动decode一次,所以要encode两次, java也是这样吗? 通常都是使用request.getParameter("paramname")获取参数数据,在request对象得到的数据都是经过解码过的,而解码过程中程序里是无法指定的.有很多新手说用request.setCharacterEncoding("字符集")可以指定解码方式,其实是不可以的,看servlet的官方API说明有对此方法的解释:Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader(). 可以看出: 在doGet()方法中, 这样的方式是无能为力的。 明白什么情况了,request.setCharacterEncoding只对post方法有效, |
|
返回顶楼 | |