2016-03-01 92 views
0

我在我的html中有一個鏈接,並且在點擊ajax時會調用某些參數。 Ajax依次調用其中調用unix腳本的servlet。Ajax不止一次地被調用

發生的問題是unix腳本被多次調用。 我還添加了警報框以瞭解它是如何多次調用的,但警報僅發生兩次(如預期的那樣)。

我已經嘗試了多個選項,如stopEventPropagation,使用一個標誌來限制ajax的調用,如果一個已經在運行。但似乎沒有任何工作。請幫忙。

下面是我的ajax調用函數和html鏈接。

var activityRunning = false; 
 

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

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

 

 

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

回答