2017-06-13 37 views
1

我有一個jQuery的使用,從一個servlet如何從ajax成功函數調用方法?

<script type="text/javascript"> 
      $(document).ready(function() { 

       $.ajax({ 
       url:'ServiceToFetchDocType', 
       type:'post', 
       cache:false, 
       success: function(response){ 
       //some data fetched from ServiceToFetchDocType 
       //Need to invoke another method here 
} 
      }); 


      </script> 

獲取一些數據是否有可能調用成功函數內部的另一種方法,並得到一些價值AJAX? 我對jQuery和ajax非常陌生,任何幫助表示讚賞。

+1

https://stackoverflow.com/questions/22233650/jquery-nested-ajax-calls-formatting – tech2017

+2

是什麼阻止你調用方法? – Satpal

+0

是的,你可以。 'succes:function(response){myFunction(response); });'... – TCHdvlp

回答

3
$(document).ready(function() { 

     $.ajax({ 
     url:'ServiceToFetchDocType', 
     type:'post', 
     cache:false, 
     success: function(response){ 
      /* invoke your function*/ 
      yourFunction(); 
     } 
    }); 
1
<script type="text/javascript"> 
     $(document).ready(function() { 

      $.ajax({ 
      url:'ServiceToFetchDocType', 
      type:'post', 
      cache:false, 
      success: function(response){ 
      Myfunction(); //this is how you can call function 
} 
     }); 
Myfunction(){ 
alert("hiii") 
} 
} 
     </script> 
// thats how it will work 
0

你可以做這樣的事情

var invokeAfterSuccess = function() { 

} 

var successFunction = function(response) { 
    /* do something here */ 
    invokeAfterSuccess() 
} 

$.ajax({ 
    url:'ServiceToFetchDocType', 
    type:'post', 
    cache:false, 
    success: successFunction 
}) 

/*--------------- OR -------------*/ 

$.ajax({ 
    url:'ServiceToFetchDocType', 
    type:'post', 
    cache:false 
}).done(successFunction)