【问题标题】:How to retrieve an array from an AJAX call?如何从 AJAX 调用中检索数组?
【发布时间】:2018-04-27 22:29:03
【问题描述】:

我进行了一个 Ajax 调用,需要返回(成功后:...)一个表示 SQL 查询的多维数组:

$.ajax({ url: 'http://localhost/Project/ajaxmethod.php',
                        data: {action: this.title},
                        type: 'post',
                        success: function(data) {

                                    //I need to use the data array here.
                        });

这里是被调用的方法:

<?php

    $bdd = new PDO('mysql:host=localhost;dbname=DATABASE;charset=utf8', 'root', '');


    $req = $bdd->query("SELECT column1, column2, column3 FROM TABLE ");

    $data = ... //Here I need to transform the previous request into a multidimensional array. 
               //For exemple data[3][2] will contain the value within the third row of the second column

    echo $data; //returning the array
}

谢谢

【问题讨论】:

    标签: php sql ajax


    【解决方案1】:

    问题

    您正在尝试返回 am 数组。但是由于 AJAX 调用适用于 HTTP 协议,因此您通常只能传输文本。因此,您的 ajaxmethod.php 将打印数组和呈现的页面,即文本将在显示时返回,而不是数组。

    解决方案

    使用 json_encode() (PHP) 将数组转换为 JSON 对象并返回。然后使用 JSON.parse() (JavaScript) 在进行 ajax 调用的页面上对其进行解码。这将给出一个数组。

    代码

    $.ajax({ url: 'http://localhost/Project/ajaxmethod.php',
                            data: {action: this.title},
                            type: 'post',
                            success: function(data) {
    
                                        //I need to use the data array here.
       var array = JSON.parse(data); //This is the array you want
    });
    

    ...

    <?php
    
        $bdd = new PDO('mysql:host=localhost;dbname=DATABASE;charset=utf8', 'root', '');
    
    
        $req = $bdd->query("SELECT column1, column2, column3 FROM TABLE ");
    
        $data = ... //Here I need to transform the previous request into a multidimensional array. 
                   //For exemple data[3][2] will contain the value within the third row of the second column
    
        echo json_encode($data) //returning the array
    }
    

    【讨论】:

      【解决方案2】:

      你不能只回显一个数组(你不能,它是一个数组而不是一个简单的值)。

      幸运的是,有一种叫做 JSON 的东西。在 PHP 中,您将所需的所有信息(在本例中为数据库中的行)存储在一个大数组中,然后执行echo json_encode($array);

      在 Javascript 端,您将 $.ajax 更改为 $.getJSON,以便 jQuery 也能理解我们在谈论 JSON,等等,您的 PHP 数组有一个不错的 javascript 版本。


      // I am not familiar with PDO, but something along these lines:
      $allData = $req->fetchAll();
      echo json_encode($allData); // Now you output a JSON version of the array
      

      然后是javascript

      $.getJSON(
          'http://localhost/Project/ajaxmethod.php',
          {action: this.title},
          function(response) {
              // And in 'response' you now have a ready to use JS object)
              console.log(response);
       });
      

      *你也可以使用$.ajax(),如果你真的想要,只需添加一个dataType: 'json

      【讨论】:

        猜你喜欢
        • 2016-08-16
        • 2015-07-18
        • 2014-09-10
        • 1970-01-01
        • 1970-01-01
        • 2013-02-11
        • 2013-01-25
        • 2012-01-04
        • 2015-12-30
        相关资源
        最近更新 更多