admin管理员组

文章数量:1195282

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.

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.

Share Improve this question asked Jun 13, 2017 at 12:23 Zeus07Zeus07 1641 gold badge5 silver badges18 bronze badges 4
  • 1 stackoverflow.com/questions/22233650/… – tech2017 Commented Jun 13, 2017 at 12:24
  • 3 What's stopping you from invoking the method? – Satpal Commented Jun 13, 2017 at 12:25
  • success: myMethod()? – apires Commented Jun 13, 2017 at 12:25
  • You can give your own function to success. success can take any function with response – Dinesh undefined Commented Jun 13, 2017 at 12:26
Add a comment  | 

4 Answers 4

Reset to default 17
$(document).ready(function() {
  $.ajax({
    url: 'ServiceToFetchDocType',
    type: 'post',
    cache: false,
    success: function(response) {
      /* invoke your function*/
      yourFunction();
    }
  });
});

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)
<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
 [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]

本文标签: javascriptHow to invoke a method from ajax success functionStack Overflow