【问题标题】:Simple application php ajax not working [closed]简单的应用程序 php ajax 不工作 [关闭]
【发布时间】:2023-03-30 18:17:01
【问题描述】:

我做了一个简单的应用程序 php & ajax,但我不知道为什么它不起作用。

我的代码:

<?php

$grId  = $_GET["groupId"];
$limit = $_GET["limit"];

if ($limit <= 0) {
  $limit = 10;
}

$servername = "localhost";
$username   = "root";
$password   = "root";
$dbname     = "productsdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql  = "SELECT id, displayName, role, groupId FROM user where groupId = ? limit ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $grId, $limit);
$stmt->execute();
$stmt->bind_result($id, $displayName, $role, $groupId);
$users = array();

while($stmt->fetch()) {
  $user              = new StdClass();
  $user->id          = $id;
  $user->displayName = $displayName;
  $user->role        = $role;
  $user->groupId     = $groupId;
  array_push($users, $user);
}

echo json_encode($users);

$stmt->close();
$conn->close();
?>

json-client-get.html:

<html>

<head>
  <script src="jquery-2.1.4.js"></script>
  <script>
    $(document).ready(function() {
      $('#students').hide();
      $("#getStudentsButton").click(function() {
        $.ajax({
          dataType: "json",
          type: "GET",
          url: "getStudentsJson.php",
          data: {
            groupId: 1,
            limit: 7
          },
          success: renderTable
        });
      });

      function renderTable(data) {
        var trHTML = '';
        $.each(data, function(i, item) {
          trHTML += '<tr><td>' + item.id + '</td><td>' + item.displayName + '</td><td>' + item.role + '</td> <td> ' + item.groupId + ' </td></tr>';
        });

        $('#students').append(trHTML);
        $('#students').show();
      }

    });
  </script>
</head>

<body>

  Lista studentilor din grupul 1:
  <div id="maindiv">
    <table id="students" border="1">
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Role</th>
        <th>GroupId</th>
      </tr>
    </table>
  </div>
  <input id="getStudentsButton" type="button" value="Load students" />
</body>
</html>

我在 phpmyadmin 中创建了一个新的数据库 productsdb,其中包含表用户和插入的单个值。

当我运行 localhost/folder/json-client-get.html 并按下按钮加载学生时,什么都没有发生。

编辑(与我的数据库合影,但我不知道为什么照片不起作用)

【问题讨论】:

  • 成功后进入renderTable()函数??你有没有在这个函数中通过警报检查过?
  • 从你的 while 循环中取出这个 -> $user = new StdClass();
  • console.log(data)readerTable 函数中。
  • success: renderTable 行应该是成功的:function(){//renderTable}
  • @Hirdesh Vishwdewa 没有任何变化

标签: php jquery mysql json ajax


【解决方案1】:

我测试了您的代码,但您遇到的 SQL 存在问题:

$sql = "SELECT id, displayName, role, groupId FROM user where groupId = ? limit ?";

检查表结构并确保您拥有这些字段

请务必在您的 phpmyadmin 中运行此查询并查看结果

SELECT id, displayName, role, groupId FROM user where groupId = 1 limit 7

【讨论】:

  • 它在 phpmyadmin 中成功运行,它为我提供了带有值的行。我试图为我的数据库制作一张照片,但无法将其放在这里
  • 我检查了你的代码几次,当我将查询更改为无效的 sql 时才看到错误
  • 当更正我的 sql 它工作正常
  • 使用他的想法,关于如何确定错误。为此,您可以使用 firebug 或 chrome 中的网络。
  • 将您的 while 代码更改为 array_push($users, array($id, $displayName,$role, $groupId));并再次检查,删除您的 while 的其他部分
【解决方案2】:

此答案不会解决您的问题,而只是向您展示如何调试 ajax 调用。我认为你会从知道如何解决它而不是解决它中受益更多。

看看下面的ajax调用:

$(document).ready(function(e) {
    $.ajax({
        url: 'ajaxScript.php',
        type: 'POST',
        data: {
            'somevar': 'somevalue'
        },
        success: function(result)
        {
            alert('Successfully called');
        },
        error: function(exception)
        {
            alert('Exeption:'+exception);
        }
    })
})

【讨论】:

  • 和/或学习如何使用您的浏览器开发者工具。
猜你喜欢
  • 1970-01-01
  • 2021-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多