簡體   English   中英

通過http請求從變量檢索數據

[英]Retrieving data from a variable through http request

我是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。 是否有一種更雄辯的方法來獲取該數組?

您將響應類型設置為“文檔”,因此您將獲得HTMLDocument作為響應。 因此,如果您只需要獲取數據變量,請嘗試通過以下方式更改代碼:javascript:

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

和PHP:

<?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腳本不包含任何html標記,而僅包含php代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM