通过http请求从变量中检索数据

时间:2017-06-07 03:50:03

标签: javascript php ajax xmlhttprequest

我是http请求和php的新手。我有一个http:

xmlhttp.onreadystatechange = function() {
       if (this.readyState == 4 && this.status == 200) {
         var doc = xmlhttp.response;
         myFunc(doc);
       }
   };
   xmlhttp.open("GET",some.php",true);
   xmlhttp.responseType = "document";
   xmlhttp.send(null);

php文件:

<!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title></title>
      </head>
      <body id="idk">
        <?php
        include("include1.inc");
        include_once("include2.inc");

        $cxn = mysqli_connect($host,$user,$passwd,$dbname) or die("Could not connect to the server at this time.");
        $table = 'table';

        $data = retrieveData($table, $cxn);//selects data from mysql db return array
        var_dump($data);

         ?>
      </body>
    </html>

如何从数据库中获取保存数组的数据变量? 当我转储或打印变量时,数组传递给`responseText。有没有更有说服力的方法来抓住那个阵列?

1 个答案:

答案 0 :(得分:0)

您将响应类型设置为&#39; document&#39;,因此您将获取HTMLDocument onbject作为响应。因此,如果您只需要获取数据变量,请尝试以这种方式更改代码: 的javascript:

<?php
        include("include1.inc");
        include_once("include2.inc");

        $cxn = mysqli_connect($host,$user,$passwd,$dbname) or die("Could not connect to the server at this time.");
        $table = 'table';

        $data = retrieveData($table, $cxn);//selects data from mysql db return array
        echo json_encode($data);
?>

和php:

output.sort(lambda element: element[1])

请注意,在这种情况下,php脚本不包含任何html标记,只包含php代码。