Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="ISO-8859-1">
  5. <title>REST JPA example with MAVEN</title>
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  7. </head>
  8. <body>
  9. <div id="id01"></div>
  10.  
  11. <script>
  12. var xmlhttp = new XMLHttpRequest();
  13. var url = "materials";
  14.  
  15. xmlhttp.onreadystatechange = function() {
  16. if (this.readyState == 4 && this.status == 200) {
  17. var myArr = JSON.parse(this.responseText);
  18. myFunction(myArr);
  19. }
  20. };
  21. xmlhttp.open("GET", url, true);
  22. xmlhttp.send();
  23.  
  24. function myFunction(arr) {
  25. var out = "<select id=\"materials\">";
  26. var i;
  27. for(i = 0; i < arr.length; i++) {
  28. out += "<option name=\""+ arr[i].id +"\">" + arr[i].name + "</option>";
  29. }
  30. out += "</select>";
  31. console.log(out);
  32. document.getElementById("id01").innerHTML = out;
  33. }
  34.  
  35. function restJson(){
  36. var material ={};
  37. material.code = document.getElementById("txt1").value;
  38. material.name = document.getElementById("txt2").value;
  39. material.price = document.getElementById("txt3").value;
  40. console.log(material);
  41. $.ajax({
  42. url: '/insertMaterial',
  43. type: 'POST',
  44. contentType: 'application/json',
  45. data: JSON.stringify(material),
  46. dataType: 'json'
  47. });
  48. }
  49. </script>
  50. <hr>
  51. <h2>Enter new material</h2>
  52. Koodi<input type="text" id="txt1" name="code"><br>
  53. Nimi<input type="text" id="txt2" name="name"><br>
  54. Hinta<input type="text" id="txt3" name="price"><br>
  55. <button onclick="restJson()">Lisää uusi materiaali</button>
  56. </body>
  57. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement