您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

jquery中文乱码的多种解决方法_jquery

2024/3/16 6:33:11发布24次查看
1、使用$.ajax出现的中文乱码的解决方案:
复制代码 代码如下:
var _realname = $(input[name='_searchname']).val();
    var termcourseid = '';
    var classid = '';
    var url = /addressbook/studentlistnopage.do;
    //var dataurl = formmap.termcourse_id=+termcourseid+&formmap.class_id=+classid+&formmap.is_online=all&formmap.realname=+_realname;
    $.ajax({
           type: post,
           url: url,
           datatype:json,
           data: {
               formmap.termcourse_id:termcourseid,
               formmap.class_id:classid,
               formmap.is_online:all,
               formmap.realname:encodeuri(_realname)
           },
           contenttype: application/x-www-form-urlencoded; charset=utf-8,
           success: function(data){
               data = eval(data);
               var html = ;
               $(#allunselecteduser).html(html);
           },
           error : function(xmlhttprequest, textstatus, errorthrown){
               alert(textstatus);
           }
        });
其中当使用dataurl中的&方式提交时,无论前台是使用encodeuri或者encodeuricomponent又或者escape把中文转码,提交到action中都是乱码,并不是想要的%e6%b1%89%e5%ad%97这种转后编码。即使加上contenttype也不行。把dataurl中的&方式提交修改为data:{name:value}的方式提交即可。
在action中使用urldecoder.decode(realname, utf-8)来转码即可转换为中文了。使用utf-8是因为jquery的提交方式默认为utf-8,即使把contenttype中的charset修改其他,例如gbk,后台也把utf-8修改gbk,都不能正确转换。
jquery ajax乱码问题解决
一、测试环境
jquery:1.3.2
tomcat:5.5.17
二、测试方法
1.使用get方式
服务器端java代码:
复制代码 代码如下:
string name = new string(request.getparameter(name).getbytes(iso8859-1),utf-8);
客户端js代码:
复制代码 代码如下:
$.ajax({url: 2.jsp,type: get,data: {name:中文},success: function(response){
        alert(response);
}});
结果:正确显示
复制代码 代码如下:
$.ajax({url: 2.jsp,type: get,data: name=中文,success: function(response){
        alert(response);
}});
结果:乱码
复制代码 代码如下:
$.get(2.jsp, { name: 中文 },function(response){
    alert(response);
});
结果:正确显示
复制代码 代码如下:
$.get(2.jsp, name=中文,function(response){
    alert(response);
});
结果:乱码2.post方式
服务器端java代码:
复制代码 代码如下:
request.setcharacterencoding(utf-8);
string name = request.getparameter(name);
客户端js代码:
复制代码 代码如下:
$.ajax({url: 3.jsp,type: post,data: method=testajaxpost&name=中文,success: function(response){
    alert(response);
}});
结果:正确显示
复制代码 代码如下:
$.ajax({url: 3.jsp,type: post,data: {name:中文},success: function(response){
    alert(response);
}});
结果:正确显示
复制代码 代码如下:
$.post(3.jsp, { name: 中文 },function(response){
    alert(response);
});
结果:正确显示
复制代码 代码如下:
$.post(3.jsp, name=中文,function(response){
    alert(response);
});
结果:正确显示
三、使用filter
复制代码 代码如下:
public void dofilter(servletrequest request, servletresponse response,
        filterchain chain) throws ioexception, servletexception {
    httpservletrequest req = (httpservletrequest) request;
    if (req.getheader(x-requested-with) != null && req.getheader(x-requested-with).equalsignorecase(xmlhttprequest)) {
        request.setcharacterencoding(utf-8);
    } else {
        request.setcharacterencoding(gbk);
    }
    chain.dofilter(request, response);
}
jquery在使用ajax的时候会在header中加入x-requested-with,值为:xmlhttprequest,filter中判断是jquery的ajax请求时就把字符编码设为utf8,这样可以解决post提交中的中文乱码问题,不需要在代码中设置request.setcharacterencoding(utf-8);
对于get方式的中文乱码问题,建议不使用get方式提交中文,统统改为post ^-^
为了和prototype.js处理中文的方式一致,可以使用如下的方式,自定义header中的属性requesttype
复制代码 代码如下:
$.ajax({
    url: 3.jsp,
    type: post,
    data: {name:中文},
    beforesend: function(xmlhttprequest){
        xmlhttprequest.setrequestheader(requesttype, ajax);
        alert(开始);
    },
    success: function(data, textstatus){
        alert(data);
    },
    error: function(xmlhttprequest, textstatus, errorthrown){
        alert(错误: + textstatus);
    },
    complete: function(xmlhttprequest, textstatus){
        alert(完成: + textstatus);
    }
 });
filter代码如下:
复制代码 代码如下:
public void dofilter(servletrequest request, servletresponse response,
        filterchain chain) throws ioexception, servletexception {
    httpservletrequest req = (httpservletrequest) request;
    if (req.getheader(requesttype) != null && req.getheader(requesttype).equalsignorecase(ajax))) {
        request.setcharacterencoding(utf-8);
    } else {
        request.setcharacterencoding(gbk);
    }
    chain.dofilter(request, response);
}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product