繁体   English   中英

来自两个函数的Ajax回调

[英]Ajax callback from two functions

您好,我对ajax有问题,两个回调代码看起来像这样

loadTasks(function(tasks) {
    taskhtml = whatStatus(tasks);
    loadSupportList(function(tasks) {
        supporthtml = support(tasks);
        $("#devtask-table").html(
                titleDraw() + "<tbody>" + supporthtml + taskhtml
                        + "</tbody>");
        hideCurtain();
    });
});

当loadSupportList或loadTasks有记录时,它工作正常。 但是当其中之一没有出现错误时,status = parsererror,抛出错误:SyntaxError:输入意外结束。 它跳过了功能,让我一无所有。 我的ajax函数看起来像这样:

function loadSupportList(callback) {
    $.ajax({
        type : "POST",
        url : url,
        data : {
            userId : userId,
            sessionId : sessionId
        },
        success : function(data) {
            callback(data['tasks']);
        },
        error : function(jqXHR, textStatus, errorThrown) {
            console.log("Error, status = " + textStatus + ", " + "error thrown: "
                    + errorThrown);
        }
    });
}

而且我不知道要更改什么,要忽略(?)还是用('success')做一些json? 有办法吗? 谢谢你的时间。

我做一些愚蠢的事情,但是做起来并不愚蠢

    loadTasks(function(tasks) {
    taskhtml = whatStatus(tasks);
    $("#devtask-table").html(
            titleDraw() + "<tbody>" + supporthtml + taskhtml
                    + "</tbody>");
    loadSupportList(function(tasks) {
        supporthtml = support(tasks);
        $("#devtask-table").html(
                titleDraw() + "<tbody>" + supporthtml + taskhtml
                        + "</tbody>");
        hideCurtain();
    });
});

假设任务返回一个数组...像这样更新您的loadSupportList函数。

    success : function(data) {
        var ret = [];
        if(
            'object' == typeof data && 
            'undefined' != typeof data.tasks
        ) ret = data.tasks;
        callback(ret);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM