gpt4 book ai didi

javascript - 我怎样才能在同一个函数中收到多个 ajax 响应

转载 作者:行者123 更新时间:2023-12-01 00:37:13 24 4
gpt4 key购买 nike

这是我的简单代码

  $.ajax({
url:'action.php',
method: 'POST',
data:{getcart:1},
success:function(response){
$('#getcart').html(response);//want to display $return_value (from action page)
$('#getcart2').html(response);//want to display $return_value2 (from action page)
}
});

我在这里向 action.php 发送请求

如果在 action.php 中,我分别回显了两个变量。

$return_value = " //Some html here ";  echo $return_value;
$return_value2= "//some other html"; echo $return_value2;

所以问题是在 ajax 中,我有带参数 response 的函数。我将如何能够从 php 接收这两个变量并将其显示在不同的 div 中。我希望你们帮助我。谢谢。

最佳答案

以 JSON 格式发送响应。

在 PHP 中

$return = array( $return_value, $return_value2 );
echo json_encode($return);

在 JavaScript 中

var response = JSON.parse(response);
$("#getcart").html(response[0]);
$("#getcart2").html(response[1]);

关于javascript - 我怎样才能在同一个函数中收到多个 ajax 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45862027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com