Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript can';在ajax成功中无法从ajax调用中获得结果_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript can';在ajax成功中无法从ajax调用中获得结果

Javascript can';在ajax成功中无法从ajax调用中获得结果,javascript,jquery,ajax,Javascript,Jquery,Ajax,我知道这已经被问过好几次了,我也读过一些关于回调的答案,但我仍然很困惑如何应用于我的情况。我有一个ajax函数,它是在第二个ajax函数成功后调用的。嵌入式ajax的结果总是未定义的 Jquery: $('#mergeModal').on('show.bs.modal', function (e) { // get the list of groups $.ajax({ type: "GET",

我知道这已经被问过好几次了,我也读过一些关于回调的答案,但我仍然很困惑如何应用于我的情况。我有一个ajax函数,它是在第二个ajax函数成功后调用的。嵌入式ajax的结果总是未定义的

Jquery:

$('#mergeModal').on('show.bs.modal', function (e) {
            // get the list of groups
            $.ajax({
                type: "GET",
                url: '@Url.Action( "GetGroups", "Tally" )',
                data: { contractId: $("#contractId").val()  },
                success: function (data) {
                    // empty then refill groups table
                    $("#groupsTable tbody").empty();
                    for (var key in data.groups) {
                        var groupParts = data.groups[key].split("::");
                        $("#groupsTable tbody").append("<tr id='" + groupParts[0] + "'><td>" + groupParts[1] + "</td><td>" + groupParts[2] + "</td></tr>");
                    }
                    // highlight first row and show appropriate students in students table
                    $("#groupsTable tbody tr:first").css("background-color", "#FFFF7F");
                    var groupID = $("#groupsTable tbody tr:first").attr('id');
                    console.log("groupID: " + groupID);
                    var students = getStudents(groupID);
                    console.log("students: " + students);
                    if(students != "false") {
                        for(var student in students.sellers) {
                            console.log(student);
                        }
                    }

问题隐藏在
getStudents
函数中,因为该函数不返回任何值。ajax调用中的两个返回将异步触发。但是您可以添加自己的回调参数/函数来获取所需的数据

函数getStudents(组,回调){ $.ajax({ 键入:“获取”, url:'@url.Action(“GetSellers”、“Tally”), 数据:{groupId:group,contractd:$(“#contractd”).val()}, 成功:功能(数据){ 回调(数据); }, 错误:函数(){ 回调(假); } });
}不要使用“true”和“false”字符串,使用布尔值
true
false
。你真的应该使用承诺而不是回调。我同意。然而,在这个问题的背景下,我决定展示上述问题的解决方案。以简单明了的方式。承诺是下一步应该考虑的清洁和适当的方式。所以我觉得我的答案不值得投否决票。谢谢!我在回拨电话的问题上忙得不可开交。我现在没有时间学习一些全新的东西,比如承诺。
function getStudents(group) {
            $.ajax({
                type: "GET",
                url: '@Url.Action( "GetSellers", "Tally" )',
                data: { groupId: group, contractId: $("#contractId").val()  },
                success: function (data) {
                    return data;
                },
                error: function() {
                    return "false";
                }
            });
        }