【问题标题】:How can i receive more than one responses in ajax in the same function如何在同一个函数中接收多个 ajax 响应
【发布时间】:2017-08-24 12:45:16
【问题描述】:

这是我的简单代码

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

我在这里向 action.php 发送请求

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

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

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

【问题讨论】:

标签: javascript php jquery mysql ajax


【解决方案1】:

以 JSON 格式发送响应。

在 PHP 中

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

在 Javascript 中

var response = JSON.parse(response);
$("#getcart").html(response[0]);
$("#getcart2").html(response[1]);

【讨论】:

  • @spaceman 先生,在我的代码中有 while 循环,我需要在循环内回显 $return_value 并在循环外回显 $return_value2 .....但是我应该在哪里回显 json_encode($return ) .如果我在循环内回显它没有得到任何数据,但如果我在循环外回显它;不是通过while循环获取重复数据而是获取$return_value2。
  • 不要在循环内回显 $return_value,而是将其保存到变量中。 while(...) { ... $return_value = 1; ... } $return_value2 = 2; 唯一需要发生的回声是echo json_encode($return);,它绝对应该在循环之外。
  • 在哪里创建一个数组?
【解决方案2】:

你可以从动作中返回一个 json

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

然后在你的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)
            }
        });

【讨论】:

    【解决方案3】:

    您需要使用json_encode 才能获取多条记录或数据。

    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);
    ?>
    

    然后在 JS

    中读取你的变量
    dataType: 'json',
    success:function(response){
        console.log(response.var1);
        console.log(response.var2);
        $('#getcart').html(response.var1);
        $('#getcart2').html(response.var2);
    }
    

    【讨论】:

      【解决方案4】:

      您可以使用json_encode 创建如下对象:

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

      然后将dataType: 'json' 添加到您的ajax 选项中。 response 然后将是一个对象,如:

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

      您可以在哪里访问特定值,例如response.return_value2 从而将它们分开。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-04
        • 1970-01-01
        • 2017-08-22
        相关资源
        最近更新 更多