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条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看