成功ajax请求但无法查看我的数据

时间:2016-05-17 00:12:57

标签: javascript php jquery mysql ajax

所以我尝试使用jQuery.ajax从MySQL数据库调用数据,但是我无法在我的网络中看到结果。我有estacionamiento table here。然后我的api.php文件和index.php分别

<?php
  include("connect.php");
  $link=Connection();

  $result = mysql_query("SELECT `estado` FROM `estacionamiento` ", $link);     //query
  $array = mysql_fetch_row($result);                          //fetch result

  echo json_encode($array);

?>
  <script src="js/jQuery.js"></script>
  <script type="text/javascript">

  $(function ()
  {
    //-----------------------------------------------------------------------
    // 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
    //-----------------------------------------------------------------------
    $.ajax({
      url: 'api.php',                  //the script to call to get data
      data: "",                        //you can insert url argumnets here to pass to api.php
                                       //for example "id=5&parent=6"
      dataType: 'json',                //data format
      success: function(data)          //on recieve of reply
      {
        alert('Successfully called');
        console.log(data);
        $('#output').html("<b> estado: </b>"+data);
        }
    });
  });

  </script>

我也能看到成功调用的警报,但在控制台日志中显示数据为[“”],但显然我在'estado'中有一些数据 你可以在这里看到结果。 (http://estacionamiento.site88.net/

编辑:现在我必须获取许多变量,我会收到任何建议

我在想这样的事情,但它没有用

<?php
  include("connect.php");
  $link=Connection();
  $data= array();
  $result = mysqli_query($link, "SELECT * FROM `estacionamiento' ORDER BY timeStamp DESC",
  while ($array = mysqli_fetch_array($result)){
    $data[] = $array;
  }

  echo json_encode($data);

?>

1 个答案:

答案 0 :(得分:0)

我猜您的it('scores an empty word as zero',function() { expect(new score('')).toEqual(0); }); 只会返回api.php。或者您只想返回一个数据?

["libre"]

然后,在你的ajax上(因为,你将返回一个不是数组的单个数据,我删除了<?php /* ESTABLISH CONNECTION; REPLACE NECESSARY PARAMETERS BELOW */ $link = new mysqli("Host", "Username", "Password", "Database"); /* CHECK CONNECTION */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $stmt = $link->prepare("SELECT estado FROM estacionamiento WHERE estado <> '' ORDER BY estado DESC"); /* PREPARE YOUR QUERY */ $stmt->execute(); /* EXECUTE QUERY */ $stmt->bind_result($estado); /* BIND RESULT TO THIS VARIABLE */ $stmt->fetch(); /* FETCH RESULT */ $stmt->close(); /* CLOSE PREPARED STATEMENT */ echo $estado; ?> ):

dataType: 'json'

也许你会对jQuery.get感兴趣,因为你只收到一个数据:

$.ajax({
    url: 'api.php',  
    success: function(data)
    {
        alert('Successfully called');
        console.log(data); /* WILL RETURN AS `libre` */
        $('#output').html("<b> estado: </b>"+data); /* #output WILL HAVE `estado: libre` */
    }
});