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 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择