weixin_33728708 2015-01-19 11:14 采纳率: 0%
浏览 23

在ajax内部调用ajax

I'm trying to make an ajax call that gets a type of a property. Using that type I then pass it into another ajax call. I'm having some difficulty doing this asynchronously because I'm trying to defer til the first property is loaded.

function getEnt_PodType() {
    var ent_PodType;
    var oDataUrl = //URL to my data;
    return $.ajax({
        url: oDataUrl,
        type: "GET",
        async: true,
        beforeSend: function (xhr) {
            xhr.setRequestHeader("ACCEPT", accept);
        },
        success: function (xhr, textStatus) { 
            var res = xhr;
            if (res.d.results != undefined) {
                ent_PodType = res.d.results[0].Ent_PodType;
            }
            console.log("The ent pod type value is "+ ent_PodType);
            return ent_PodType;
        }
    });
}

function getProjects() {

    var QUERY_FILTER = getEnt_PodType().done(function (result) {
        "$filter=Ent_PodType eq '" + result + "'";
    });

    var url = restUrl + QUERY_FILTER;

    console.log("The url form getProjects is " + QUERY_FILTER);

    return $.ajax({
        url: url,
        type: "GET",
        async: true,
        beforeSend: function (xhr) {
            xhr.setRequestHeader("ACCEPT", accept);
        },
        success: function (xhr, textStatus) {
            projects = parseODataResultTest(xhr);
            return projects;
        }
    });

}

When I call the getProjects(), I thought building the url would wait for getEnt_PodType() to return its value but it doesn't seem to work that way.

Instead it goes ahead and executes the rest of getProjects(). Is there any way to do this asynchronously?

  • 写回答

5条回答 默认 最新

  • 普通网友 2015-01-19 11:19
    关注

    The ajax call needs to be in the done promise. Something like the following should be ok.

    function getProjects() {
    
        return  getEnt_PodType().done(function (result) {
            var QUERY_FILTER = "$filter=Ent_PodType eq '" + result + "'";
            var url = restUrl + QUERY_FILTER;
    
            console.log("The url form getProjects is " + QUERY_FILTER);
    
            return $.ajax({
                url: url,
                type: "GET",
                async: true,
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("ACCEPT", accept);
                },
                success: function (xhr, textStatus) {
                    projects = parseODataResultTest(xhr);
                    return projects;
                }
        });
        });
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件