嵌套的AJAX调用不起作用

时间:2012-08-13 17:47:36

标签: javascript jquery xml ajax

我无法让嵌套的AJAX调用正常工作。我想要的是在外部AJAX调用成功完成后,执行内部AJAX调用。

var diningHours = $("#diningHours");
var facStaffDiningData = $("#facStaffDiningData");
var diningCommonsData = $("#diningCommonsData");
if($.trim(diningHours.html()).length == 0) {
    var season;
    //This call executes fine (tested it with console logging)
    $.get("data/dining-hours.php", {summer: "check"}, function(seasonData, seasonStatus) {
        if(seasonStatus == "success") {
            season = seasonData;
            //This is the call that isn't being executed
            $.get("data/dining-hours.php", function(hoursData, hoursStatus) {
                if(hoursStatus == "success") {
                    var hours = $(hoursData).find("hours dining");
                    var html = hoursFeed(hours, season);
                    diningHours.append(html).collapsibleset("refresh");
                }
            });
        }

    });
}

我做错了吗?

3 个答案:

答案 0 :(得分:0)

我认为seasonStatus是多余的,因为回调将在成功时执行。

这应该有效

var season;
    //This call executes fine (tested it with console logging)
    $.get("data/dining-hours.php", {summer: "check"}, function(season, seasonStatus) {
           console.log('CB1',season);
            $.get("data/dining-hours.php", function(hoursData) {
                    console.log('CB2',hoursData);
                    var hours = $(hoursData).find("hours dining");
                    var html = hoursFeed(hours, season);
                    diningHours.append(html).collapsibleset("refresh");

            });
        }

    });

答案 1 :(得分:0)

我会尝试这样的事情:

var diningHours = $("#diningHours"),
    facStaffDiningData = $("#facStaffDiningData"),
    diningCommonsData = $("#diningCommonsData");

if(!$.trim(diningHours.html()).length) {
    var XHR = $.get("data/dining-hours.php", {summer: "check"});
        XHR.success(function(seasonData) {
            var season = seasonData,
                XHR2 = $.get("data/dining-hours.php");
                XHR2.success(function(hoursData) {
                    var hours = $(hoursData).find("hours dining"),
                        html = hoursFeed(hours, season);
                    diningHours.append(html).collapsibleset("refresh");
                });
        });
}​

问题是,hours dining究竟是什么,以及您希望find()函数如何找到它?

答案 2 :(得分:0)

深入研究这个问题,我发现了问题的真正根源。 XML文档有一堆编码错误(数据中有保留和版权符号等内容)。删除这些并用正确的实体替换它们可以解决问题。我认为这个问题的原始代码现在完全正常。