3

私は ajax 成功関数から値を返そうとしています。しかし、それは何も返していません。

JS

function calculate_total_percentage(course_log_id){
    var total_percentage = 0;
    $.ajax({
        url:"teacher_internal_exam_management/get_exams_of_course_log/"+course_log_id,
        type: "POST",
        dataType: "json",
        success: function (exams_of_course_log) {
            for (var x = 0; x < exams_of_course_log.length; x++) {
                total_percentage += parseInt(exams_of_course_log[x].marks_percentage);
            }
            alert(total_percentage);
            return total_percentage;
        }
    });
}

私がそう呼んだら

alert(calculate_total_percentage(course_log_id));

次に「61」を表示します(alert(total_percentage);の呼び出しによる)が、「未定義」を表示するのはなぜですか?'61' が 2 回表示されますか? 何が問題ですか?

4

1 に答える 1