malformed URI sequence

来源:互联网 发布:美食杰网站seo分析 编辑:程序博客网 时间:2024/05/02 01:37

这几天在做服务端发送HTML文本到客户端,然后客户端解析编码并显示。发生了“Malformed URI sequence”和“被编码的URI不是合法的编码”这两个错误。

在Firefox 3.6下,错误如下:

在IE 6下,错误如下:


我使用的是ajax + Serlvet技术,在Eclipse3.5下开发没有问题,只要部署到Tomcat中就要出问题。为什么会出现这个问题呢?

ajax解析服务端响应数据,使用decodeURIComponent方法。在客户端测试多次发现是服务端的数据不对。
原来是过时方法java.net.URLEncoder.encode(String s);因为使用JSON格式返回到客户端,必须要编码,否则会造成JSON格式错误。
应该使用
java.net.URLEncoder.encode(String s, "UTF-8");


-------------------------------------------------------------------------------------------------------------------------------------------------

前端解决方案


 /**     * @param {String}     * @return {String}     */    var decodeURIComponentEx = function(uriComponent){if(!uriComponent){return  uriComponent;}var ret;try{ret = decodeURIComponent(uriComponent);}catch(ex){ret = unescape(uriComponent);}return ret;    };




原创粉丝点击