ajax弹出窗口被拦截

       对于ajax中弹出窗口,浏览器会默认拦截。

       项目中对于一条记录,先用ajax到后台请求判断是否可以修改,前台页面根据返回值若可修改则新开一个模态对话框对已有数据进行修改。初期代码如下:

function onUpdate(xh){
	check();
}

function check(){
	 var jsonObj = {"domain.xh":xh};
	 var url = "/a/b!check";  
	 ajaxCommon(url,jsonObj, "checkSuc"); 
}

function checkSuc() {
	var retVal = data.domain.retVal;
	if (retVal/1 > 0) {			
		alert("对不起,不能修改!");
	}else {
		var url ="/a/b!initMx.action?domain.xh="+xh;
		window.showModalDialog(url,window,"dialogHeight:650px;dialogWidth:880px;center:yes;resizable:no;status:no;scroll:yes;help:no;minimize:yes;")
	
	}
}

function ajaxCommon(url,data,doSuccessFucName){
	url = url + ".action";
	$.ajax({
			   type: "POST",
			   url: url,
			   dataType:"json",
			   data: data,
			   contentType: "application/x-www-form-urlencoded; charset=GBK", 
			   beforeSend:function(){
			   },
			   complete:function(){			   		
			   },
			   success: function(data){	
					try{
						if(typeof(eval(doSuccessFucName)) =="function"){
							eval(doSuccessFucName+"(data);");
						}
					}catch(e){							
					}
			   },
			   error:function(data){			   		
			   }
	 });  
}

       结果当可修改弹出窗口时被浏览器拦截。

       最后想到个办法,将ajax设置为同步,在页面设置一个变量,ajax返回时更改这个变量值,当ajax执行完成后,再判断该值,若可修改,则弹出窗口。更改后代码如下:

function onUpdate(xh){
	check();
}
function check(){
	 var jsonObj = {"domain.xh":xh};
	 var url = "/a/b!check";  
	 ajaxCommon(url,jsonObj, "checkSuc",false); 
	 afterCheck();
}
function afterCheck() {
	var retVal = $("#retVal").val();
	if (retVal/1 > 0) {			
		alert("对不起,不能修改!");
	}else {
		var url = "/a/b!initMx.action?domain.xh="+xh;
		window.showModalDialog(url,window,"dialogHeight:650px;dialogWidth:880px;center:yes;resizable:no;status:no;scroll:yes;help:no;minimize:yes;")
	
	}
}
function checkSuc() {
	$("#retVal").val(data.domain.retVal);
}
function ajaxCommon(url,data,doSuccessFucName,asyncFlag){
	asyncFlag = asyncFlag == undefined ? true : asyncFlag;
	url = url + ".action";
	$.ajax({
			   type: "POST",
			   async:asyncFlag,
			   url: url,
			   dataType:"json",
			   data: data,
			   contentType: "application/x-www-form-urlencoded; charset=GBK", 
			   beforeSend:function(){
			   },
			   complete:function(){			   		
			   },
			   success: function(data){	
					try{
						if(typeof(eval(doSuccessFucName)) =="function"){
							eval(doSuccessFucName+"(data);");
						}
					}catch(e){							
					}
			   },
			   error:function(data){			   		
			   }
	 });  
}

 结果,判断正常,弹出窗口正常。

 

你可能感兴趣的:(Ajax)