Ajax/Jquery - 在从服务器返回数据时调用 javascript 函数/使用 ajax 执行并行 SQL 查询

Ajax/Jquery - Call javascript function upon data being returned from server/ Parallel SQL query execution using ajax

本文关键字:使用 函数 javascript ajax 并行 查询 SQL 调用 执行 Jquery 服务器      更新时间:2023-09-26

我有一个PHP值数组。我遍历它,通过迭代 for 循环对服务器进行 ajax 调用(我完全理解在 for 循环中制作 ajax cal 不是一个好主意。毫无疑问,以贝特方式执行此操作的任何建议/指示都会很棒。我想寻求这个问题的答案,但请。来自服务器的数据根据要处理和返回的内容花费不同的时间。

我遇到的问题是showCustomerUsageChart函数调用在任何阶段都不会被调用。我尝试通过在此函数的条目上设置断点来调试它,并看到根本没有调用它。但是,我可以看到服务器在相应的数据可用时在不同的时间点返回 JSON 数据。毫无疑问,我知道我错误地实现了这一点。

AJAX世界对我来说并不是太熟悉。我在这个论坛上查找了不少问题,但是,有点难以将这些碎片放在一起。

我可以请求帮助以实现我所追求的吗?我将非常感谢任何帮助。

<script>
    var ajaxurl = 'myDbHandler.php';
    <?php for ($k = 0; $k < count($propertyCustomer); $k++) { ?>
        data = {
            'param1': paramFromUser1, 
            'param2': paramFromUser2, 
            'param3': paramFromUser3,
            'lookUpParamId': "<?php echo $k ?>"
        };
        $.post(ajaxurl, data,function (response) {
            var resp = $.parseJSON(response);
            showCustomerUsageChart(
                "<?php echo $propertyCustomer[$k][0] ?>",
                "<?php echo $propertyCustomer[$k][1] ?>",
                "<?php echo $propertyCustomer[$k][2] ?>",
                "<?php echo $propertyCustomer[$k][3] ?>",
                resp,       
            );
        });
    <?php } ?>
</script>

所以这里有一种方法可以代替它,我避免在javascript中使用php,所以代码和标记更干净。

为了模拟来自您的查询的滞后,我这样做了:

myDbHandler.php

$number = rand(2, 11);
sleep($number);
echo 'success after ' . $number . ' seconds';

原始.php文件现在如下所示(test.php(:

<?php
// Array filled with testdata:
$propertyCustomer = array(
    array('value1', 'value2', 'value3', 'value4'),
    array('value1', 'value2', 'value3', 'value4'),
    array('value1', 'value2', 'value3', 'value4')
);
// Make a string that html can handle and also encode the array above to a jsonString:
$json = htmlentities(json_encode($propertyCustomer));
?>
<!-- Echo the string as data-attribute -->
<div id="holdingElement" data-array="<?php echo $json ?>"></div>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="test.js"></script>

我将js与标记分开,因为我无法忍受丑陋的代码(test.js(:

$(document).ready(function () {
    // Get the array from the element:
    var phpArray = $('#holdingElement').data('array');
    // Console.log for testing:
    console.log(phpArray);
    // Loop through the array with the jQuery each function:
    $.each(phpArray, function(k, phpArrayValue){
        // The makeCall function returns a ajaxObject so the object gets put in var promise
        var promise = makeCall(k);
        // Now fill the success function in this ajaxObject (could also use .error() or .done() )
        promise.success(function(response){
            // When success, call the function and use the values out of the array above
            showCustomerUsageChart(
                phpArrayValue[0],
                phpArrayValue[1],
                phpArrayValue[2],
                phpArrayValue[3],
                response
            );
        });
    });
});

function makeCall(paramId) {
    // Just testdata:
    var paramFromUser1 = 'val1';
    var paramFromUser2 = 'val2';
    var paramFromUser3 = 'val3';
    // Use ajax instead of $.post to use the success function, and return this ajaxObject
    return $.ajax({
        url: 'myDbHandler.php',
        type: 'post',
        data: {
            'param1': paramFromUser1,
            'param2': paramFromUser2,
            'param3': paramFromUser3,
            'lookUpParamId': paramId
        }
    });
}
// Function to log the results from the ajax call:
function showCustomerUsageChart(val1, val2, val3, val4, response) {
    console.log(val1, val2, val3, val4, response);
}

我希望这能有任何意义,并且对您有用!

你的代码在那里有问题:

showCustomerUsageChart(
                    <?php echo $propertyCustomer[$k][0] ?>

您必须在 PHP 结束标记后加上","。