简体   繁体   English

来自两个函数的Ajax回调

[英]Ajax callback from two functions

Hello i have a problem with ajax and two callbacks code look like that 您好,我对ajax有问题,两个回调代码看起来像这样

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

And it's work fine when loadSupportList or loadTasks have records. 当loadSupportList或loadTasks有记录时,它工作正常。 But when one of them don't i get Error, status = parsererror, error thrown: SyntaxError: Unexpected end of input. 但是当其中之一没有出现错误时,status = parsererror,抛出错误:SyntaxError:输入意外结束。 And it jump off function and leave me with nothing. 它跳过了功能,让我一无所有。 My ajax function looks that way : 我的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);
        }
    });
}

And i dont know what to change, to ignore(?) or made some json with ('success')? 而且我不知道要更改什么,要忽略(?)还是用('success')做一些json? Is there a way? 有办法吗? Thanks for your time. 谢谢你的时间。

I make something silly, but its work so it's not silly 我做一些愚蠢的事情,但是做起来并不愚蠢

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

assuming tasks returns an array... update your loadSupportList function like this.. 假设任务返回一个数组...像这样更新您的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