我有一个页面试图通过 jQuery 运行 3 个连续的单独 ajax 调用。不幸的是,每次我加载页面时,只有两个 ajax 调用会运行。此外,这 3 个调用中的哪一个将运行并不一致。
这是 jQuery 代码(在 order.php 页面上):
$.ajax({
type: "Post",
url: "includes/get_product_info.php",
data: "id=7",
success: function(data) {
console.log('First One Works!');
}
});
$.ajax({
type: "Post",
url: "includes/get_product_info.php",
data: "id=6",
success: function(data) {
console.log('Second One Works!');
}
});
$.ajax({
type: "Post",
url: "includes/get_product_info.php",
data: "id=1",
success: function(data) {
console.log('Third One Works!');
}
});
这是 PHP 脚本 (get_product_info.php):
include("connection.php");
$conn = dbConnect('query');
$sql = 'SELECT *
FROM products
WHERE product_id = ' . $_POST['id'];
$result = $conn->query($sql) or die(mysqli_error($conn));
$rows = array();
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
echo json_encode($rows);
当我多次加载 order.php 页面时,有时会显示“First One Works”和“Second One Works”,但有时会显示“Third One 有效”和“First One 有效”等。这表明,它只运行每次调用两个ajax,它们是随机的,它将运行哪些。
如何以正确的顺序运行所有三个 ajax 调用?