Ajax不止一次被调用

时间:2016-03-01 14:18:04

标签: jquery ajax jsp

我的html中有一个链接,点击后会用某些参数调用ajax。 Ajax反过来调用内部调用unix脚本的servlet。

发生的问题是多次调用unix脚本。 我还添加了警告框,以了解它是如何被多次调用,但警报只是预期的两倍(有2个警报)。

我尝试过多个选项,比如stopEventPropagation,如果一个人已经在运行,则使用一个标志来限制ajax的调用。但似乎没有任何效果。请帮忙。

下面是我的ajax调用函数和html链接。

var activityRunning = false;

function collectForDeployment(event, smopId, componentId, activityId){	
	///alert 1
	alert('Outside If: '+activityRunning);

	if($('#noscript_'+componentId+activityId).length>0){
		alert('No script found to deploy');
	}else{		
		if(!activityRunning){
          ///alert 2
			alert('Inside If: '+activityRunning);
			checkForUserNamePass(componentId, "<%=request.getSession().getAttribute("CONTEXT_ROOT").toString()%>/ajaxservlet?action=actDeployment&smopId="+smopId+"&componentId="+componentId+"&activityId="+activityId+"&environment="+$('#environment').val(),function(urlString){	
				//alert(urlString);	
			   	$.ajax({
			        url: urlString,        
			        method: 'post',	
			        tryCount : 0,
			        retryLimit : 0,
			        beforeSend: function(){       
			        	$('#backgroundDiv').hide();
			           document.getElementById("divBackground").style.display="block";
			           event = event || window.event;
			   		   stopEventPropagation(event);
			        },
			        success: function(data) {  
			        	var newLocation = "<%=request.getContextPath()%>/jsp/EditSmopPopup.jsp?randomKey="+Math.random()+"&componentId="+$('#selectedComponent').val().replace('#','')+"&smopId=<%=request.getParameter("smopId")%>&environment="+$('#environment').val()+"&articleId="+$('#selectedComponent').val()+"&divId="+$('#selectedActivity').val();        	
			        	window.location.href=newLocation;
			        	$('#backgroundDiv').hide();
			    	   	var popUpObj=window.open("",
			    	   			"View Deployment Log for SMOP Id : " + smopId + " : ComponentId : " + componentId + " : Activity Id : " + activityId,
			    	   			"toolbar=no," +
			    	   			"scrollbars=no," +
			    	   			"location=no," +
			    	   			"statusbar=no," +
			    	   			"menubar=no," +
			    	   			"resizable=0," +
			    	   			"width=650," +
			    	   			"height=450," +
			    	   			"left = 350," +
			    	   			"top=170"
			    	   			);
			    	   	popUpObj.document.title="View Deployment Log for SMOP Id : " + smopId + " : ComponentId : " + componentId + " : Activity Id : " + activityId;
			    	   	popUpObj.document.body.innerHTML=data;    	   	
				   		popUpObj.focus();
				   		document.getElementById("divBackground").style.display="none";
				   		event = event || window.event;
				   		stopEventPropagation(event);
			        }
			    });
			});
		}else{
			return;
		}	
		activityRunning = true;
		event = event || window.event;
		stopEventPropagation(event);
	}
}



function checkForUserNamePass(componentId, urlString, callback){	
	var finalString;
	if($('#'+componentId+"_passreq").val()=='Y'){
		$("#divAdmin").show();
		$("#fadeAdmin").show();		
		$("#btnLogin").click(function(event){
			if($('#txtAdminUser').val() == '' || $('#txtAdminPass').val() == ''){
				alert('Please enter the Admin user name or password to proceed');
				event = event || window.event;
				stopEventPropagation(event);
			}else{
				finalString = urlString+"&adminuser="+$('#txtAdminUser').val()+"&adminpass="+$('#txtAdminPass').val();
				$("#divAdmin").hide();
				$("#fadeAdmin").hide();		
				event = event || window.event;
				stopEventPropagation(event);
				callback(finalString);
			}
		 	
		});
	}else{
		finalString = urlString;
		callback(finalString);
	}	
}
<a href="javascript:void(0);"
					id="a_<%=dto1.getComponentId()+dto2.getActivityId()%>"
					onclick="collectForDeployment(event, '<%=request.getParameter("smopId")%>', '<%=dto2.getComponentId()%>', '<%=dto2.getActivityId()%>')"><img
						border="0" alt="Deploy Individual Activity"
						title="Deploy Individual Activity"
						src="<%=request.getSession()
											.getAttribute("CONTEXT_ROOT")
											.toString()%>/images/deploy.png"
						width="20px"></a>

1 个答案:

答案 0 :(得分:0)

这里似乎解释了这个问题...... http://geek.starbean.net/?p=393