Advertisement
avr39ripe

jsAJAXPOST

May 21st, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en" id="html">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>XHR</title>
  6. <style>
  7. html,
  8. body {
  9. display: flex;
  10. align-items: center;
  11. justify-content: center;
  12. }
  13.  
  14. .container {
  15. display: flex;
  16. flex-direction: column;
  17. justify-content: center;
  18. align-content: center;
  19. align-items: center;
  20. margin-top: 30px;
  21. position: relative;
  22. }
  23.  
  24. .mainDiv {
  25. display: flex;
  26. flex-direction: row;
  27. }
  28.  
  29. table, tr, td {
  30. border: 1px solid black;
  31. border-collapse: collapse;
  32. padding: 5px;
  33. }
  34.  
  35. table {
  36. width: 70%;
  37. margin: 10px;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <div id="container" class="container">
  43. <input type="number" id="userId">
  44. </div>
  45. <script>
  46. 'use strict'
  47.  
  48. //function getUser()
  49. let url = 'https://jsonplaceholder.typicode.com/users';
  50. let method = 'POST';
  51. let newUser = { id: 2607, name: 'avr39-ripe', company: 'itstep' };
  52.  
  53. let xhr = new XMLHttpRequest();
  54.  
  55. xhr.open(method, url);
  56. xhr.setRequestHeader('Content-type','application/json; charset=utf-8')
  57. xhr.addEventListener('load', function () {
  58. if (this.status >= 400) {
  59. console.log(`HTTP ERROR CODE: ${this.status}!`);
  60. }
  61. else {
  62. console.log(this.responseText);
  63. }
  64. });
  65.  
  66. xhr.send(JSON.stringify(newUser));
  67. </script>
  68. </body>
  69. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement