GET请求的替代方案 发送GBK编码的URL

来源:互联网 发布:淘宝寄快递怎么算钱 编辑:程序博客网 时间:2024/05/19 00:54
/*js本身提供了三个函数可以进行编码:escape : 按照unicode进行编码,但是编码出来的不是url格式,需要处理。encodeURI: 按照UTF-8进行编码,但是只能按照UTF-8编码。只编码参数。encodeURIComponent: 按照UTF-8进行编码,但是只能按照UTF-8编码,并且编码路径和参数。对于国内的大多数网站和企业应用来说,开发过程中都是用GBK编码,用户浏览器提交请求也使用GBK编码。在jsp中对于这个问题很容易处理,一般来说setCharacterEncoding就能解决问题,不需要手工编码解码,除了sendRedirect的url中的中文参数是个例外。对于asp.net应用程序来说,由于asp.net提供了更为强大的类库支持,所以这也不是问题,asp就比较麻烦了(刚学,可能了解的不多)。js本身的encodeURI只提供一个参数,不能指定编码类型,asp的Server.HTMLEncode只能encode没有decode,对于客户端来说,是发送UTF-8编码的URL还是GBK的URL这都是有可能的。比如通过直接写URL的方式。页面中的链接一般形如: <a href="a.html">a</a>假设参数中包含中文: <a href="a.html?a=中">a</a>在我的机器上接收到的参数是乱码。环境: 中文Vista IIS7.0 asp.net asp 开启父路径、DEBUG,其他设置都是默认使用HTTP代理监视了一下走的协议,发现a.html?a=中完全是GBK编码,没有做任何处理,IIS接收到的是乱码。而使用a.html?a=%d6%d0则没有问题使用form提交,get和post都没有问题使用a.html?a=%E4%B8%AD也是乱码说明服务端只能处理url经过GBK编码过的如果要完全避免乱码,只能在服务端对编码类型进行检查,google似乎就是这么做的,google都支持。但检查编码比较麻烦。那么只能简单的使用form提交了,使用如下函数可以简单的替代,但是如果客户端的form提交方式也是UTF-8的话,那么提交的数据依然是UTF-8,如果客户端的form提交方式采用的是GBK编码的话,那么提交的数据就是GBK编码了。*/var Util = {};Util.Post = function(args){    var _url = args.url;    var _target = args.target;    var _method = args.method;    var _postBody = args.postBody;    var _debug = args.debug;        var oForm = document.getElementById("_j2ee_form_object");    if(oForm == null || oForm == undefined)    {        oForm = document.createElement("Form");        oForm.id = "_j2ee_form_object";                if(true == _debug)        {            oForm.style.cssText = "width: 200px; height: 200px; font-size: 12px; display: display;";        }        else        {            oForm.style.cssText = "width: 0px; height: 0px; font-size: 0px; display: none;";        }        document.body.appendChild(oForm);    }    else    {        oForm.innerHTML = "";    }    if(_target != null && _target != undefined)    {        oForm.target = _target;    }    oForm.action = _url;    if(_method != null)    {        oForm.method = _method;    }    else    {        oForm.method = "POST";    }    if(_postBody != null && (typeof(_postBody) + "").toLowerCase() == "object")    {        for(var i in _postBody)        {            var _name = "" + i;            var _valu = _postBody[i];            var _type = (true == _debug ? "text" : "hidden");            if(typeof(_valu) == "string")            {                var _txt = document.createElement("INPUT");                                _txt.type = _type;                _txt.name = _name;                _txt.value = _valu;                oForm.appendChild(_txt);            }            else if(typeof(_valu) == "object" && (_valu instanceof Array))            {                for(var j = 0; j < _valu.length; j++)                {                    var _txt = document.createElement("INPUT");                                        _txt.type = _type;                    _txt.name = _name;                    _txt.value = _valu[j];                    oForm.appendChild(_txt);                }            }        }    }    if(true == _debug)    {        var _txt = document.createElement("INPUT");                        _txt.type = "text";        _txt.name = "_Util_Post_DEBUG";        _txt.value = "Util.Post.DEBUG: true";        oForm.appendChild(_txt);    }    if(oForm.submit != null && (true != _debug))    {        oForm.submit();    }    else    {        // TODO: something    }    return (args.returnValue == true)}function DoAction(){    Util.Post({            url: "a.html",             target: "NewWindow",             method: "GET",             postBody: {a: "中国", b: "是个伟大的国家 !", c: "是的 !"},            debug: true,            returnValue: false        }    );}
原创粉丝点击