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

Ajax get data to get another ajax data

本文关键字:Ajax 数据 获取 另一个      更新时间:2023-09-26

我需要一些关于组合嵌套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代码

function GetDirectories(choreid, callback) {
    var theurl = webapiurl + '/Models' + getTopUrlVars()["model"] + '/ChoreProcess(' + choreid + ')?$expand=Process';
    $.ajax({
        type: "GET",
        url: theurl,
        data: {},
        dataType: "json",
        success: function (response) {
            callback(response["TM1.ChoreProcessesProcess"][0].ID);
        },
        failure: function (msg) {
            alert(msg);
        }
    });
}

有可能做到这一点吗?我尝试使用全局变量,但第一个值是"。

如果在getDirectories中有返回,请尝试使用变量

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, 
            });