dongwu9647 2018-04-12 14:25
浏览 111
已采纳

php + jquery + ajax获取多个值

I am working with jQuery AJAX. Using an AJAX call I invoke a PHP page. By default it returns a single value to the success function of AJAX. However I want to retrieve multiple data individually. What can I do?

Here's the jQuery code with AJAX , which returns a single value:

function getTime(){
  $.ajax({
    type: "POST",
    url: "test.php",
    data: {
      fd: sdate,
      sd: edate
    },
    dataType: "text",
    success: function(msg) {
      $("#results").text(msg);
    }
  });
};

Test.php:

echo $days;
echo $hours;
echo $minutes;
echo $seconds;
  • 写回答

3条回答 默认 最新

  • dongzhuifeng1843 2018-04-12 14:30
    关注

    Typically to return multiple responses, the easiest way to do this is to return a json encoded array. Something like the following should work.

    echo json_encode( array(
        "days" => $days,
        "hours" => $hours,
        "minutes" => $minutes,
        "seconds" => $seconds
    ) );
    

    Since your ajax request has dataType: "text" you will need to parse the response to use it with JSON.parse(msg), however if you changed it to dataType: "json" you would not have to do this step as jQuery will try to auto parse it for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 链表栈表达式求值求解
  • ¥15 关于龙芯1b,JTAG停止调试服务
  • ¥20 收一个快手协议下单算法
  • ¥15 神途脚本逐鹿神途,管理自欺欺人
  • ¥15 求一个图片中的成交量选股公式
  • ¥15 已知正方形内随机生成坐标matlab
  • ¥30 关于#python#的问题:我想要的是这79个大特征对于房屋售价的最大的影响前十名(相关搜索:随机森林)
  • ¥15 react native新项目运行时遇到问题
  • ¥15 脚本运行Python文件,报错:error: unrecognized
  • ¥15 使用matlab计算自定义特殊函数的二重积分,改变积分顺序所得的结果不同的问题?