簡體   English   中英

如何從ajax成功函數調用方法?

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

我有一個帶有 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>

是否可以在成功函數中調用另一個方法並獲得一些值? 我對 jQuery 和 ajax 非常陌生,任何形式的幫助表示贊賞。

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

你可以做這樣的事情

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