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

如何在同一个函数中的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.

  • 写回答

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]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料