0

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();
    });
});
6
  • 2
    Can you describe your problem in other way? I don't understand it correctly Commented Jan 20, 2016 at 12:39
  • what does loadTasks look like? on which one are you getting the error ? Commented Jan 20, 2016 at 12:41
  • I have table that have records from two diffrent source and connect them at $("#devtask-table").html(here) but when there is no records in for example loadSupportList() then it jump of loadSupportList(function(tasks) {} and never get to devtask-table. Commented Jan 20, 2016 at 12:45
  • Its looke same as loadSupportList only url change. Commented Jan 20, 2016 at 12:45
  • Use "Pause on Exceptions" in Chrome Developer Tools (with Async box checked!) so that the debugger can stop exactly where the problem is. Commented Jan 20, 2016 at 12:46

1 Answer 1

1

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);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

it still leave loadSupportList(function(tasks){} tasks returns json.
technically raw json would either evaluate to an object or an array. you mean it returns a json string or what?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.