7

I've a jQuery with ajax using to fetch some data from a servlet

    <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>

Is it possible to invoke another method inside the success function and get some value? I've very new to jQuery and ajax, any kind of help is appreciated.

Zeus07
  • 168
  • 1
  • 5
  • 17

4 Answers4

17
$(document).ready(function() {
  $.ajax({
    url: 'ServiceToFetchDocType',
    type: 'post',
    cache: false,
    success: function(response) {
      /* invoke your function*/
      yourFunction();
    }
  });
});
septianw
  • 121
  • 1
  • 6
guest
  • 706
  • 7
  • 15
5

you can do something like this

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)
Semi-Friends
  • 480
  • 5
  • 17
2
<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
-4
 [success: function (data) {
                                TypeOfReportDropdown.closest("form").find("div\[id$='MonitoringChemicalData'\]")\[0\].innerHTML = data;
                                var hours = $("#UptimeHourYear").val();
                                var emissions = round(parseFloat(($("#AverageMassLoadOut").val() * hours) / 1000), 1);
                                $("#Emissions").val(emissions);
                                $("#TotalEmissions").val(emissions); 

                                $(this).delay(3000).queue(function () {
                                    var emissieTotal = 0;
                                    var totalHAP = 0;

                                    $('\[data-emissie\]').each(function () {
                                        emissieTotal += Number($(this).data('emissie'));
                                        var hap = $(this).data('hap');
                                        if (hap == "True") {
                                            totalHAP += Number($(this).data('emissie'));
                                        }
                                    });

                                    var emissieFinalTotal = round(emissieTotal, 3);
                                    $('#TotalEmissionForOnlineInstruments').html(emissieFinalTotal);

                                    var totalHAPFinal = round(totalHAP, 3);
                                    $('#TotalHAPForOnlineInstruments').html(totalHAPFinal);
                                }); 
                            }][1]
  • Please provide some explaination instead of just code. – Klaus Dec 26 '20 at 13:56
  • Hi Aashish! Please write some text explaining how your code might answer the question, then rather just pasting code into an answer. Also, when using example code in either an answer or question, it is always preferred to use minimal examples (https://stackoverflow.com/help/minimal-reproducible-example) that is short and doesn't contain more code than necessary to answer the question. Have a great day! :) – Olov Dec 26 '20 at 13:57