json 使用Ajax和分页进行动态数据重载

jfewjypa  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(121)

我正试图从一个API中获取有关动物及其速度的信息。
x1c 0d1x的数据
信息应该保存在一个有5列的表中。它应该看起来像这样:



你会在下面找到我写的代码。问题是只有两个按钮加载,但没有信息表。你知道错误可能是什么吗?

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Test</title>
  6. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
  7. integrity="sha384-1srTifKwwmIWHsyIaapyeJQY2XxzfjIHpftPvwHSi97Mfm4cZNKDBAdVSrtRvti5" crossorigin="anonymous">
  8. <script> src = "https://code.jquery.com/jquery-3.7.1.min.js"
  9. integrity = "sha384-1H217gwSVyLSIfaLxHbE7dRb3v4mYCKbpQvzx0cegeju1MVsGrX5xXxAvs/HgeFs"
  10. crossorigin = "anonymous"
  11. </script>
  12. </head>
  13. <body class="text-center">
  14. <br />
  15. <button class="btn btn-primary btn-lg" id="prev">Prev</button>
  16. <button class="btn btn-primary btn-lg" id="next">Next</button>
  17. <br />
  18. <table id="table" class="table table-striped table-striped table-hover">
  19. <tbody id="tableBody" />
  20. </table>
  21. <script src=" myScript.js">
  22. </script>
  23. </body>
  24. </html>

字符串
//在myScript.js下面

  1. $(document).ready(function () {
  2. /*Variablen */
  3. var counter = -5;
  4. var end = false;
  5. $("#next").click(function () {
  6. if (!final) {
  7. counter = counter + 5;
  8. }
  9. $.get("http://localhost:8080/webService/api/velocitiespagination?start=" + counter, function (velocities) {
  10. createTable(velocities);
  11. });
  12. });
  13. $("#prev").click(function () {
  14. counter = counter - 5;
  15. if (counter < 5){
  16. counter = 0;
  17. }
  18. $.get("http://localhost:8080/webService/api/velocitiespagination?start=" + counter, function (velocities) {
  19. createTable(velocities);
  20. });
  21. });
  22. createTable(velocities);
  23. });
  24. function createTable(velocities) {
  25. var text = "<tbody><tr><th>Tier<>/th><th>Maximalgeschwindigkeit Tier</th></tr>";
  26. $.each(velocities, function (key, velocity) {
  27. text = text + "<tr><td>" + velocity.animal + "</td><td>" + velocity.velocity + "</td></tr>";
  28. });
  29. text += "</tbody>"
  30. $("#myTable").html(text);
  31. end = velocities.length === 0;
  32. }

jgzswidk

jgzswidk1#

我想如果你查看控制台,你会看到关于$(...).ready()不是函数的错误,因为你没有加载jQuery。
在执行此操作时,您将声明变量srcintegritycrossorigin

  1. <script> src = "https://code.jquery.com/jquery-3.7.1.min.js"
  2. integrity = "sha384-1H217gwSVyLSIfaLxHbE7dRb3v4mYCKbpQvzx0cegeju1MVsGrX5xXxAvs/HgeFs"
  3. crossorigin = "anonymous"
  4. </script>

字符串
您希望将它们作为script标记上的属性,如下所示

  1. <script src = "https://code.jquery.com/jquery-3.7.1.min.js"
  2. integrity = "sha384-1H217gwSVyLSIfaLxHbE7dRb3v4mYCKbpQvzx0cegeju1MVsGrX5xXxAvs/HgeFs"
  3. crossorigin = "anonymous" >
  4. </script>

展开查看全部

相关问题