您的位置:首页 > Web前端 > JQuery

两种方式解决jquery Ajax 发送中文乱码的方法,

2012-09-10 10:24 387 查看
    查过网上有很多方法,但很多都不成功,可能是因为在不环境下的区别吧!

首先,必须保证前台后台的编码统一,其次在连接数据库时的url也要指定编码。



第一:简单直接,修改页面

data:{ username:function(){return ""+name;} },必须以这种方式传递参数。name

是要传的参数。后台获取request.getParameter("username");这个username是function前面的username

function conn(name,moduleName,url,url2){
	 var b;
	 if(name != moduleName){
		 $.ajax({
			    type: "post", //使用get方法访问后台
			    dataType: "json", //返回json格式的数据
			    async: false, //同步   不写的情况下 默认为true
			    contentType: "application/x-www-form-urlencoded; charset=utf-8",
			    url: "${ctx}/sys/"+url+"-"+url2+"-isExist.action", //要访问的后台地址
			    data:{
			    	name:function(){return ""+name;}
			    	},
			    success: function(data,status){
					if(data){//名称存在时data是false
						b = true;
					}else{
						alert("名称已经存在!!!");
						b = false;
					}
				}
			});




第二:使用的是进行两次encodeURI..再发送!



function isFile(){
			
			var fname = document.getElementById("upload").value;
			var type = document.getElementsByName("type");
			 for(var i=0;i<type.length;i++){
	        	  if(type[i].checked==true){
	        		  type = type[i].value;
	        	  }
	        }
			fname = fname.substring(fname.lastIndexOf("\\")+1,fname.length)
			fname = encodeURI(encodeURI(fname)); 
			
			$j = jQuery.noConflict();//解决JQUERY冲突
		//Jquery ajax异步
		//	var url= "<%=request.getContextPath()%>/controlDocument/isName.do?fileName="+fname+"&type="+type;
		//	$j.post(url,function(data){
		//		if(data == "YES"){
		//			b="000";
		//			alert("该文档已经存在!覆盖后不可恢复,确定在覆盖吗?"+b);
		//		}
		//		
		//	},"text");
		
		//Jquery ajax 同步
			$j.ajax({ 
				async: false, 
				type : "POST", 
				url : "<%=request.getContextPath()%>/controlDocument/isName.do?fileName="+fname+"&type="+type, 
				dataType : 'text', 
				success : function(data) { 
				if(data == "YES"){
					if(window.confirm("该文档已经存在!覆盖后不可恢复,确定在覆盖吗?")){
						bool = "1";
					}else{
						bool = "0";
					}
				}
				} 
				}); 
			
		}
        </script>



以下是jsp代码



   

<div align="left">
		<fieldset class="fiel_cx">
			<legend>文件上传</legend>
			<form action="/pms/controlDocument/upload.do" method="POST"
				enctype="multipart/form-data">
				<font color="red">指定文件查看人员:</font> <select id="role" name="role">
					<option value="">请选择</option>
					<c:forEach items="${kpRoleList}" var="role">
						<option value="${role.roleid}">${role.rolelabel}</option>
					</c:forEach>
				</select> 上传类型:<input type="radio" name="type" value="1">文档 <input
					type="radio" name="type" value="2">视频 <br> <br>
				文件上传 <input type="file" name="upload" id="upload" /> <input
					type="submit" name="fileCaption" value="上   传"
					onclick="return sub();" />
			</form>
		</fieldset>
	</div>





后台接收时,要encode一下.



getResponse().setContentType("application/json;charset=utf-8");
		getResponse().setHeader("caChe-Control", "no-cache");
		getResponse().setCharacterEncoding("UTF-8");
		getRequest().setCharacterEncoding("UTF-8");
		String fileName = URLDecoder.decode(
				getRequest().getParameter("fileName"), "UTF-8");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: