繁体   English   中英

所有ajax调用(动态的ajax调用)都收到响应后,调用函数是JQuery

[英]Calling a function is JQuery after all ajax calls(number of ajax calls are dynamic) have received responses

HTML:-

结果

Javascript:-

 $(document).ready(function(){ $("#result").html("function started here"); var requests = Array(); requests.push($.get('https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA')); requests.push($.get('https://maps.googleapis.com/maps/api/geocode/json?address=EGL, Bangalore')); var defer = $.when.apply($, requests); console.log(defer); defer.done(function(){ $("#result").html("Completed");    // This is executed only after every ajax request has been completed    $.each(arguments, function(index, responseData){ // alert(index); $("#result").html(responseData);        // "responseData" will contain an array of response information for each specific request    }); }); }); 

上面的代码段仅在所有ajax调用均返回“成功”响应时才有效。 有什么办法可以让我知道所有ajax调用是否都得到响应,可能是“成功” /“失败”。

您可以ajaxStart,ajaxStop,ajaxError,ajaxSuccess。

 $(document).ajaxStart(function () {
            //when ajax start
        }).ajaxStop(function () {
           //when ajax stop
        }).ajaxError(function () {
            //when ajax error
        }).ajaxSuccess(function () {
        // when ajax success
    });

这将为每个ajax调用触发。

暂无
暂无

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

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