ajax.jsp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: 武恒
  4. Date: 2023/2/10
  5. Time: 11:27
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <html>
  10. <head>
  11. <title>Ajax</title>
  12. </head>
  13. <body>
  14. <div style="width: 800px; margin: 0 auto">
  15. <button onclick="sendData()" > sendData </button>
  16. <div id="ajax-respone" style="width: 800px; height: 390px; border: 1px solid red; overflow: auto;">
  17. </div>
  18. </div>
  19. <script>
  20. var sendData = function(){
  21. var xmlHttpRequest = new XMLHttpRequest();
  22. xmlHttpRequest.onreadystatechange = function () {
  23. if ( xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200 ) {
  24. //document.getElementById("ajax-respone").innerHTML = xmlHttpRequest.responseText;
  25. var parse = JSON.parse( xmlHttpRequest.responseText );
  26. for (let i = 0; i < parse.length; i++) {
  27. let str = parse[i].brandName + " "
  28. + parse[i].companyName + " "
  29. + parse[i].description + " "
  30. + parse[i].id + " "
  31. var htmlSpanElement = document.createElement("p");
  32. htmlSpanElement.innerText = str
  33. document.getElementById("ajax-respone").append( htmlSpanElement )
  34. }
  35. }
  36. }
  37. xmlHttpRequest.open("GET", "<%=request.getContextPath()%>/jsonRequest", true)
  38. xmlHttpRequest.send()
  39. }
  40. </script>
  41. </body>
  42. </html>