0

我无法让嵌套的 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");
                }
            });
        }

    });
}

难道我做错了什么?

4

3 回答 3

0

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

于 2012-08-13T18:46:06.387 回答
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");

            });
        }

    });
于 2012-08-13T17:52:52.887 回答
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()函数如何找到它?

于 2012-08-13T18:01:01.930 回答