jquery 提交中文乱码问题


var params_= $("#form1").serialize();
$.ajax({
type:'post',

contentType: "application/x-www-form-urlencoded; charset=UTF-8",
dataType:"html",
url:baseURL+'/saveContractBasicInfo.do?submit=Y&skipMode=jsp'+extraParam,//+'&'+decodeURI(params_),
data:decodeURI(params_), processData:false,
success:function(data, textStatus){
//alert(data);
if(data.indexOf("submitInSign")!=-1){
submitSign();
}else{
Ext.MessageBox.alert( '提示','失败,出错');

}

},
error:function(){
//alert("ff");
Ext.MessageBox.alert( '提示','失败,出错');
}
});




[quote]
contentType String
Default: 'application/x-www-form-urlencoded'
When sending data to the server, use this content-type. Default is "application/x-www-form-urlencoded", which is fine for most cases. If you explicitly pass in a content-type to $.ajax() then it'll always be sent to the server (even if no data is sent). Data will always be transmitted to the server using UTF-8 charset; you must decode this appropriately on the server side.

[/quote]
[url=http://api.jquery.com/jQuery.ajax/]jquery 官方 API[/url]

$("#form1").serialize(); 把表单序列化成 par=value&par2=value2
decodeURI(params_)解码,因为serialize编码一次用utf-8,所以要解码一次

contentType: "application/x-www-form-urlencoded; charset=UTF-8", 即加上这个参数后,无论是否有数据,都会编码一次

processData:false 表示在处理传送数据到服务器时,不用编码处理了

就这样,后台服务端是GBK编码的,就不会乱码了,

之前,我尝试用下面的配置,还是乱码
contentType: "application/x-www-form-urlencoded; charset=GBK",
processData:true, 默认为true,我没填

总结分析:
我猜想 contentType 参数使用说明,无论怎么样,jquery都是用utf-8编码的,
加上这个,就是告诉服务端,要自动解码用utf-8,
其实我上面的,decodeURI 不用解码一次,反正,传递数据时,也编码了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值