ITeye博客

jquery 提交中文乱码问题

喧嚣求静 · 2012-07-27 16:47 · javascript · Web前端 · Markdown


 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( '提示','失败,出错');
						              								    }
							    });



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.

jquery 官方 API

$("#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)

暂无评论。

评论功能维护中,当前仅展示历史评论。