Javascript Ajax获取数据以获取另一个Ajax数据

Javascript Ajax获取数据以获取另一个Ajax数据,javascript,jquery,ajax,asynchronous,Javascript,Jquery,Ajax,Asynchronous,我需要一些关于组合嵌套ajax调用的指针 $.ajax({ type: "GET", dataType: "json", url: generalurl + "/Chore" + objectid + "?$expand=Process", success: function (data, textStatus) { list = $(data)[0]["TM1.ChoreProcessRel"]; res = [];

我需要一些关于组合嵌套ajax调用的指针

$.ajax({
    type: "GET",
    dataType: "json",
    url: generalurl + "/Chore" + objectid + "?$expand=Process",
    success: function (data, textStatus) {
        list = $(data)[0]["TM1.ChoreProcessRel"];
        res = [];
        for (var i = 0, l = list.length; i < l; i++) {
            var e = list[i];

            GetDirectories(e.ID, function (d) {
                alert(d); //this is working fine already   
            });

            //I just want to include d on the below statement but I'm still getting undefined
            res.push({
                title: "" + e.Name, key: e.LogicalName + d, 
            });
        }
    }
});

有可能这样做吗?我尝试使用全局变量,但第一个值是“”

如果在GetDirectory中有return,请尝试使用变量

var d = GetDirectories(e.ID, function (d) {
                alert(d); //this is working fine already   
            });

            //I just want to include d on the below statement but I'm still getting undefined
            res.push({
                title: "" + e.Name, key: e.LogicalName + d, 
            });

不要在GetDirectory中使用回调,因为成功封装了ajax请求。您尝试在响应中设置res.push而不是使用callback。我尝试在成功响应中设置res.push,但它不起作用。它只是没有检测到res。我还尝试了var d=GetDirectories,在函数中,执行返回d,但这也不起作用。嗯,但是你真的需要调用嵌套的ajax请求吗?我觉得它的应用程序设计很糟糕。如果调用ajax并返回后端数据,它可以一次性返回所有数据。并设置刷新此ajax的计时器循环。这对于某些客户端(浏览器)来说可能是过度使用。如果你真的需要这样做,试着想想。
var d = GetDirectories(e.ID, function (d) {
                alert(d); //this is working fine already   
            });

            //I just want to include d on the below statement but I'm still getting undefined
            res.push({
                title: "" + e.Name, key: e.LogicalName + d, 
            });