如何在javascript中访问/打印PHP的输出?

时间:2016-09-03 20:44:42

标签: javascript php mysql json

我从jquery ajax javascript调用PHP文件。我能够在浏览器中看到PHP文件的输出。但是当通过ajax jquery调用时,我无法打印相同的输出。我怎样才能访问它?

**test.php**
<?php
    $dbuser="root";
    $dbname="test";
    $dbpass="root";
    $dbserver="localhost";
    // Make a MySQL Connection
    $con = mysql_connect($dbserver, $dbuser, $dbpass) or die(mysql_error());
    mysql_select_db($dbname) or die(mysql_error());
    // Create a Query
    $sql_query = "SELECT id, login FROM user";
    // Execute query
    $result = mysql_query($sql_query) or die(mysql_error());
    $jsonArray = array();
    while ($row = mysql_fetch_array($result)){
        $jsonArrayItem = array();
        $jsonArrayItem["id"] = $row["id"];
        $jsonArrayItem["login"] = $row["login"];
        array_push($jsonArray, $jsonArrayItem);
    //echo '<option value='. $row['id'] . '>'. $row['login'] . '</option>';
    }
    mysql_close($con);
    $tableData = array(
            "data" => $jsonArray
        );
    header('Content-Type: application/json');
    echo json_encode($tableData,JSON_UNESCAPED_SLASHES);
    die();
  ?>

的test.html

<html>
<head>
<title>Example</title>
</head>
<body>
<script  src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript">
$.ajax({
    url: "test.php",
    type: "GET",
    dataType: "json",
    data: values,
    success: function(data){
        window.alert(data);
    },
    error: function(data){
        alert("AJAX error!");
    }
})
</script>
</body>
</html>
<style>
#container {
    text-align: center;
}
a, figure {
    display: inline-block;
}
figcaption {
    margin: 10px 0 0 0;
    font-variant: small-caps;
    font-family: Arial;
    font-weight: bold;
    color: #bb3333;
}
figure {
    padding: 5px;
}
img:hover {
    transform: scale(1.1);
    -ms-transform: scale(1.1);
    -webkit-transform: scale(1.1);
    -moz-transform: scale(1.1);
    -o-transform: scale(1.1);
}
img {
    transition: transform 0.2s;
    -webkit-transition: -webkit-transform 0.2s;
    -moz-transition: -moz-transform 0.2s;
    -o-transition: -o-transform 0.2s;
}
</style>

语法有问题吗?有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

使用Chrome调试控制台。网络部分,XHR过滤器,选定的URL,响应选项卡。像这样:

1)在Chrome中按CTRL + I

2)尝试遵循:

enter image description here