【问题标题】:How to return a JSON formatted array from an ajax request?如何从 ajax 请求返回 JSON 格式的数组?
【发布时间】:2014-08-27 14:08:40
【问题描述】:

我正在构建两个相互依赖的下拉列表,其中第二个列表(“sensor_list”)的选项取决于第一个列表(“node_list”)中选择的选项。要执行此操作,请将侦听器附加到“node_list”,并且选择选项时,我会制作一个Ajax请求,以便加载'sensor_list'的数据。两个列表的所有数据都是从同一个数据库中加载的。

在该 ajax 请求中,我访问数据库上的“传感器”表,并获得所需传感器的“传感器 ID”和“传感器名称”。我现在想要的是将此数据传递回请求的“成功”功能。这是自动完成的吗?我需要向函数传递任何参数吗?

另外,我需要返回的数据是 JSON 格式,以备后用。这怎么可能?

这是 PHP 代码:

<?php

$con = mysql_connect("localhost", "root", "") or die('connection not made');
$db = mysql_select_db('name_of_db', $con) or die('db not selected');

$query = "SELECT SensorID,Variable FROM sensors WHERE SensorID IN (SELECT SensorID FROM nodesensors WHERE NodeID=node_selected)";
$result = mysql_query($query, $con) or die('query not made');

$options = json_encode($result);


?>

“node_selected”是我在请求中传递的变量,它是我在第一个列表中选择的节点的 ID。

JavaScript 是:

if (node_selected!=0 & node_selected!=null){
                $.ajax({
                    type: "POST",
                    url: "php/fetch_sensors.php",
                    data: node_selected,
                    success: function( options ){
                        ...//here I need the options to be in json format
                    }
                });
            }

“选项”是我希望从请求中返回的数据,它们将成为“传感器列表”的选项。

提前非常感谢!!

【问题讨论】:

    标签: php jquery mysql ajax json


    【解决方案1】:

    您可以为此使用mysql_fetch_assoc 函数

    尝试将 PHP 代码更改为以下内容:

    <?php
    
    $con = mysql_connect("localhost", "root", "") or die('connection not made');
    $db = mysql_select_db('name_of_db', $con) or die('db not selected');
    
    $query = "SELECT SensorID,Variable FROM sensors WHERE SensorID IN (SELECT SensorID FROM nodesensors WHERE NodeID=node_selected)";
    $result = mysql_query($query, $con) or die('query not made');
    
    $returnArray = array();
    while($row = mysql_fetch_assoc($result)) {
        $returnArray[] = $row;
    }
    
    $options = json_encode($returnArray);
    
    ?>
    

    【讨论】:

      【解决方案2】:

      改变

      $options = json_encode($result);
      

      echo json_encode($result);
      

      并将dataType:'json' 添加到您的ajax 通话中:

      $.ajax({
          type: "POST",
          url: "php/fetch_sensors.php",
          data: node_selected,
          dataType: 'json',
          success: function( options ){
              ...//here I need the options to be in json format
          }
      });
      

      【讨论】:

      • @Debflav 我明白我只是说如果它有语法错误你可以编辑答案。用户将看到您的编辑并感谢您的编辑:)
      • 我这样做了,但是没有用。另外,我警告('你好');在成功函数内部,但它从未出现过。当我尝试单独运行“fetch_sensors.php”时,除了空白页之外什么都没有。
      • @dimitris012 我建议首先关注 PHP 代码,以确保它真正打印 JSON。尝试设置error_reporting(E_ALL);,也许你有一些隐藏的警告。
      【解决方案3】:

      首先,stop using ext/mysql if you can

      您实际上并未从查询中获取结果。

      while ($row = mysql_fetch_assoc($results)) {
          $rows[] = $row;
      }
      

      然后您实际上必须将数组发送到响应。

      echo json_encode($rows);
      

      jQuery 在检测 JSON 方面非常聪明,应该自动解析它,以便您可以使用 options 作为对象。如果它不能自动工作,您可以执行以下任一操作:

      //php
      header("Content-type: application/json")
      
      //javascript
      data: node_selected,
      dataType: "json",
      
      // *or*
      success: function (options) {
          options = JSON.parse(options);
      }
      

      最后,由于此请求是幂等检索,因此您确实应该使用 GET 而不是 POST 作为方法。

      【讨论】:

        猜你喜欢
        • 2021-07-18
        • 1970-01-01
        • 2013-04-23
        • 2016-08-17
        • 1970-01-01
        • 2021-11-29
        • 1970-01-01
        • 1970-01-01
        • 2016-12-10
        相关资源
        最近更新 更多