douxianliu6756
2017-08-24 12:45
浏览 55
已采纳

如何在同一个函数中的ajax中收到多个响应

here is my simple code

  $.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)
            }
        });

Here i am sending request to action.php

and if in action.php i have echo two variables separately for example.

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

So the question is in ajax i have function with argument response . how i will be able to receive these both variables from php and display it in different divs. i hope you guys help me. thanks.

图片转代码服务由CSDN问答提供 功能建议

这里是我的简单代码

  $ .ajax({  
 url:'action.php',
方法:'POST',
 data:{getcart:1},
 success:function(response){
 $('#getcart')。html(response  ); //想要显示$ return_value(来自操作页面)
 $('#getcart2')。html(响应); //想要显示$ return_value2(来自操作页面)
} 
}); \  n   
 
 

这里我向action.php发送请求

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

  $ return_value =“//这里有一些HTML”;  echo $ return_value; 
 $ return_value2 =“//其他一些html”;  echo $ return_value2; 
   
 
 

所以问题在于ajax我有参数响应的功能。 我将如何能够从PHP接收这两个变量并将其显示在不同的div中。 i希望你们帮助我。 谢谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • douli4852 2017-08-24 12:48
    已采纳

    Send the responses as JSON.

    In PHP

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

    In Javascript

    var response = JSON.parse(response);
    $("#getcart").html(response[0]);
    $("#getcart2").html(response[1]);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drcrc28428 2017-08-24 12:50

    your could return a json from action

    echo json_encode(array('cart' => $return_value, 'cart2' => $return_value2));
    

    then in your 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)
                }
            });
    
    评论
    解决 无用
    打赏 举报
  • dougan7523 2017-08-24 12:51

    You need to use json_encode in order to get multiple records or data.

    action.php

    <?php 
    header('Content-Type: application/json');
    
    $array = array(
                   'var1'=> 'var value 1',
                   'var2'=> 'var value 2'
    // more variables to go
    );
    echo json_encode($array);
    ?>
    

    and then read your variables in JS

    dataType: 'json',
    success:function(response){
        console.log(response.var1);
        console.log(response.var2);
        $('#getcart').html(response.var1);
        $('#getcart2').html(response.var2);
    }
    
    评论
    解决 无用
    打赏 举报
  • duangouyan3328 2017-08-24 12:56

    You can use json_encode to create an object like:

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

    Then add dataType: 'json' to your ajax options. The response then will be an object like:

    {
        "return_value" => " //Some html here ",
        "return_value2" => "//some other html",
    }
    

    Where you can access the specific values with e.g. response.return_value2 and thus separate them.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题