无法从Ajax函数

时间:2018-06-18 18:17:38

标签: ajax asynchronous

我已经在Ajax功能上苦苦挣扎了一段时间,尽管我尝试了不同的尝试,但我无法在代码的重新编写中使用我的变量。

load_table('table1');

function load_table(table_name){
  $('#'+table_name).DataTable( {
    "ajax": {
      "url": "./admin_functions.php",
        "type": "POST",
        "dataType": 'json'
    },
    "drawCallback": function() {
      switch(table_name) {
        case 'table1':
          var array_loop = [[1, 1],[2, 2]];
         break;
        case 'table2':
          var policy_dropdown_json = {};
          $.ajax({
            type: "POST",
              url: "./admin_functions.php",
              data: { action: 'get_dropdown',
                "table_name": 'support'
               },
              dataType: 'json',
              success: function(result) {
                 policy_dropdown_json = JSON.stringify(result);
                 console.log(policy_dropdown_json); // Result OK. Contains JSON string
              }
         });

          console.log(policy_dropdown_json); // Result not OK. Contains empty object
          var array_loop = [[1, 1,],[2, 2, policy_dropdown_json]];
        break;
        default:
          var array_loop = [[1, 1,],[2, 2],[3, 3]];
      }

      $('#'+table_name).Tabledit({
        "url": "./admin_functions.php?table_name="+table_name,
        columns: {
          identifier: [0, 'id'],
          editable: array_loop
        }
      });
    }
  });
}

我需要在Ajax函数之外使用policy_dropdown_json,但它总是空的。我知道这是一个异步调用,添加async: false修复了它,但它已被弃用。

我尝试将Ajax代码放在单独的函数中或使用Promises(我不熟悉),但它没有帮助。例如thisthis

我使用的是Datatables和Tabledit插件。

policy_dropdown_json应该是一个JSON字符串。

1 个答案:

答案 0 :(得分:0)

你最好的选择是这样使用它:

load_table('table1');

function load_table(table_name) {
    $('#' + table_name).DataTable({
        "ajax": {
            "url": "./admin_functions.php",
            "type": "POST",
            "dataType": 'json'
        },
        "drawCallback": function() {
            switch (table_name) {
                case 'table1':
                    var array_loop = [
                        [1, 1],
                        [2, 2]
                    ];
                    break;
                case 'table2':
                    var policy_dropdown_json = {};
                    $.ajax({
                        type: "POST",
                        url: "./admin_functions.php",
                        data: {
                            action: 'get_dropdown',
                            "table_name": 'support'
                        },
                        dataType: 'json',
                        success: function(result) {
                            policy_dropdown_json = JSON.stringify(result);
                            console.log(policy_dropdown_json); // Result OK. Contains JSON string
                            var array_loop = [
                                [1, 1, ],
                                [2, 2, policy_dropdown_json]
                            ];

                            $('#' + table_name).Tabledit({
                                "url": "./admin_functions.php?table_name=" + table_name,
                                columns: {
                                    identifier: [0, 'id'],
                                    editable: array_loop
                                }
                            });
                        }
                    });
                    return; //Ajax will spawn off separately and hit this return before the ajax request ends. Once the ajax request is complete, it will use the code inside the success parameter.
                    console.log(policy_dropdown_json); // Result not OK. Contains empty object
                    var array_loop = [
                        [1, 1, ],
                        [2, 2, policy_dropdown_json]
                    ];
                    break;
                default:
                    var array_loop = [
                        [1, 1, ],
                        [2, 2],
                        [3, 3]
                    ];
            }

            $('#' + table_name).Tabledit({
                "url": "./admin_functions.php?table_name=" + table_name,
                columns: {
                    identifier: [0, 'id'],
                    editable: array_loop
                }
            });
        }
    });
}

您还会注意到,一旦ajax完成,就会创建表并且policy_dropdown_json是正确的,并且array_loop也是正确的。在ajax完成之前,这两个变量都没有设置为正确的数据。