简体   繁体   English

如何从ajax成功函数调用方法?

[英]How to invoke a method from ajax success function?

I've a jQuery with ajax using to fetch some data from a servlet我有一个带有 ajax 的 jQuery 用于从 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.我对 jQuery 和 ajax 非常陌生,任何形式的帮助表示赞赏。

$(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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM