从 @RestContoller 使用 JavaScript 获取 JSON 数据

huangapple go评论106阅读模式
英文:

Fetching JSON data from @RestContoller using JavaScript

问题

The HTML页面为何无法加载特定请求的URL并无法从JSON格式中获取数据的问题?

控制器(Controller)代码部分:

  1. @RestController
  2. @RequestMapping("api/reservations")
  3. public class ReservationController {
  4. private final ReservationService reservationService;
  5. @Autowired
  6. public ReservationController(ReservationService reservationService) {
  7. this.reservationService = reservationService;
  8. }
  9. @GetMapping("/all")
  10. public List<Reservation> showAllReservations() throws SQLException {
  11. return reservationService.showAllReservations();
  12. }
  13. }

HTML + JavaScript部分:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Reservations</title>
  7. </head>
  8. <body>
  9. <script>
  10. // 从REST控制器获取JSON数据
  11. fetch("http://localhost:8080/api/reservations/all")
  12. .then((response) => response.json())
  13. .then((data) => {
  14. // 遍历JSON数组中的每个对象
  15. var formattedData = "";
  16. data.forEach((item) => {
  17. formattedData +=
  18. "<p>Reservation ID: " +
  19. item.id +
  20. "</p>" +
  21. "<p>Customer: " +
  22. item.customer.name +
  23. "</p>" +
  24. "<p>Email: " +
  25. item.customer.email +
  26. "</p>" +
  27. "<p>Check-in Date: " +
  28. item.dateCheckIn +
  29. "</p>" +
  30. "<p>Check-out Date: " +
  31. item.dateCheckOut +
  32. "</p>" +
  33. "<hr>";
  34. });
  35. // 使用格式化数据更新HTML容器
  36. var dataContainer = document.getElementById("data-container");
  37. dataContainer.innerHTML = formattedData;
  38. })
  39. .catch((error) => {
  40. console.error("Error:", error);
  41. });
  42. </script>
  43. <div id="data-container"></div>
  44. </body>
  45. </html>

您已经检查了URL的正确性,控制器看起来也没问题,文件位于不同的文件夹中(模板用于.html文件,src>java>package用于控制器)。

英文:

Why the html page doesn't load on specific requested URL and doesn't fetching data from JSON format?
Result of opening api/reservations/all

Controller:

  1. @RestController
  2. @RequestMapping(&quot;api/reservations&quot;)
  3. public class ReservationController {
  4. private final ReservationService reservationService;
  5. @Autowired
  6. public ReservationController(ReservationService reservationService) {
  7. this.reservationService = reservationService;
  8. }
  9. @GetMapping(&quot;/all&quot;)
  10. public List&lt;Reservation&gt; showAllReservations() throws SQLException {
  11. return reservationService.showAllReservations();
  12. }
  13. }

HTML + Javascript

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;utf-8&quot; /&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
  6. &lt;title&gt;Reservations&lt;/title&gt;
  7. &lt;/head&gt;
  8. &lt;body&gt;
  9. &lt;script&gt;
  10. // Fetch JSON data from REST controller
  11. fetch(&quot;http://localhost:8080/api/reservations/all&quot;)
  12. .then((response) =&gt; response.json())
  13. .then((data) =&gt; {
  14. // Iterate over each object in the JSON array
  15. var formattedData = &quot;&quot;;
  16. data.forEach((item) =&gt; {
  17. formattedData +=
  18. &quot;&lt;p&gt;Reservation ID: &quot; +
  19. item.id +
  20. &quot;&lt;/p&gt;&quot; +
  21. &quot;&lt;p&gt;Customer: &quot; +
  22. item.customer.name +
  23. &quot;&lt;/p&gt;&quot; +
  24. &quot;&lt;p&gt;Email: &quot; +
  25. item.customer.email +
  26. &quot;&lt;/p&gt;&quot; +
  27. &quot;&lt;p&gt;Check-in Date: &quot; +
  28. item.dateCheckIn +
  29. &quot;&lt;/p&gt;&quot; +
  30. &quot;&lt;p&gt;Check-out Date: &quot; +
  31. item.dateCheckOut +
  32. &quot;&lt;/p&gt;&quot; +
  33. &quot;&lt;hr&gt;&quot;;
  34. });
  35. // Update the HTML container with the formatted data
  36. var dataContainer = document.getElementById(&quot;data-container&quot;);
  37. dataContainer.innerHTML = formattedData;
  38. })
  39. .catch((error) =&gt; {
  40. console.error(&quot;Error:&quot;, error);
  41. });
  42. &lt;/script&gt;
  43. &lt;div id=&quot;data-container&quot;&gt;&lt;/div&gt;
  44. &lt;/body&gt;
  45. &lt;/html&gt;

I checked URL correctness, controller seems fine, the files locates in different folders (templates for .html and src>java>package for controller).

答案1

得分: 0

问题出在粗心大意上,点击按钮将我带到了api/reservation/all而不是reservations.html,所以JavaScript无法运行,也无法显示结果。感谢你的答案。

英文:

The problem was inattention, clicking on the button took me to api/reservation/all instead of reservations.html and so the javascript didn't work and didn't show the result. Thanks for your answers.

huangapple
  • 本文由 发表于 2023年5月23日 01:50:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定