Javascript 在同一个函数中,如何在ajax中接收多个响应

Javascript 在同一个函数中,如何在ajax中接收多个响应,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,这是我的简单代码 $.ajax({ url:'action.php', method: 'POST', data:{getcart:1}, success:function(response){ $('#getcart').html(response);//want to display $return_value (from action page)

这是我的简单代码

  $.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中,我有两个单独的echo变量

$return_value = " //Some html here ";  echo $return_value;
$return_value2= "//some other html";  echo $return_value2;
所以问题是在ajax中,我有一个带有参数响应的函数。如何从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]);

将响应作为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]);

您可以从操作返回json

echo json_encode(array('cart' => $return_value, 'cart2' => $return_value2));
然后在你的js中

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

您可以从操作返回json

echo json_encode(array('cart' => $return_value, 'cart2' => $return_value2));
然后在你的js中

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

您需要使用
json\u encode
来获取多个记录或数据

action.php


您需要使用
json\u encode
来获取多个记录或数据

action.php

可以使用创建类似以下对象的对象:

$array = [
    "return_value" => " //Some html here ",
    "return_value2" => "//some other html",
];
echo json_encode($array)
{
    "return_value" => " //Some html here ",
    "return_value2" => "//some other html",
}
然后将
数据类型:“json”
添加到ajax选项中。
响应
将是如下对象:

$array = [
    "return_value" => " //Some html here ",
    "return_value2" => "//some other html",
];
echo json_encode($array)
{
    "return_value" => " //Some html here ",
    "return_value2" => "//some other html",
}
您可以通过响应来访问特定值。返回值2从而将它们分开。

您可以使用创建一个对象,如:

$array = [
    "return_value" => " //Some html here ",
    "return_value2" => "//some other html",
];
echo json_encode($array)
{
    "return_value" => " //Some html here ",
    "return_value2" => "//some other html",
}
然后将
数据类型:“json”
添加到ajax选项中。
响应
将是如下对象:

$array = [
    "return_value" => " //Some html here ",
    "return_value2" => "//some other html",
];
echo json_encode($array)
{
    "return_value" => " //Some html here ",
    "return_value2" => "//some other html",
}

您可以通过响应来访问特定值。返回值2,然后将它们分开。

查看从PHP返回数组或对象。查看从PHP返回数组或对象。@spaceman先生,在我的代码中有while循环,我需要在循环内回显$return\u value,在循环外回显$return\u value2…..但是在这里我应该在哪里回显json\u encode($return)。如果我在循环内回显,没有得到任何数据,但如果我在循环外回显;不通过while循环获取重复数据,而是获取$return\u值2。不要在循环中回显$return\u值,而是将其保存到变量中<代码>而(…){…$return\u value=1;…}$return\u value=2需要发生的唯一回显是
echo json_encode($return)
它肯定应该在循环之外。在哪里创建数组?@spaceman先生,在我的代码中有while循环,我需要在循环内部回显$return\u值,在循环外部回显$return\u值2……但是在这里我应该在哪里回显json\u编码($return)。如果我在循环内回显,没有得到任何数据,但如果我在循环外回显;不通过while循环获取重复数据,而是获取$return\u值2。不要在循环中回显$return\u值,而是将其保存到变量中<代码>而(…){…$return\u value=1;…}$return\u value=2需要发生的唯一回显是
echo json_encode($return)并且它肯定应该在循环之外。在哪里创建数组?