简体   繁体   中英

Ajax callback from two functions

Hello i have a problem with ajax and two callbacks code look like that

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. But when one of them don't i get Error, status = parsererror, error thrown: SyntaxError: Unexpected end of input. And it jump off function and leave me with nothing. My ajax function looks that way :

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')? 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..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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