Use function within ajax Response Success

Asked

Viewed 360 times

0

I have a list of tasks with checkboxes, which when marked updates, via ajax, the status of the task in the database, what I want to do is return the completion percentage of the list, which is already done in the backend, and also already has a function js called "percentage", the problem is that in checkbox marking Success I would like to take the value of the percentage function, but there is no return.

Ex:

function porcentagem(id) {

    $.ajax({
        url: 'url',
        method:'POST',
        dataType: 'JSON',
        data: {id: id},
        success: function(data) {
            if(data.success == 1) {
                return data.porcentagem;
            }
        }

    });
}

Success: ajax of the checkbox:

                if(data.success == 1) {
                    var id = $("input[name='id']").val();
                    alert( porcentagem(id) );
                }

The percentage function was set before ajax, and yet the console shows me:

Uncaught Typeerror: percentage is not a Function

  • Tried to take the return of Ajax in a variable and display after that the variable itself ?

  • but in this case the variable of the percentage function would not be local?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.