Advertisement
aymen_laroussi

PostRequest

Oct 25th, 2023
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.76 KB | Cybersecurity | 0 0
  1. <button onclick="postData()">Click me</button>
  2. <script>
  3. function postData() {
  4.   const data = {
  5.     id: 7,
  6.     userId: 122,
  7.     category: "MEUBLES",
  8.     description: "At dolor sint ab si",
  9.     name: "Austin Odonnell",
  10.     expirationDate: "1979-12-06",
  11.     status: "NONDISPONIBLE",
  12.     organizationId: null
  13.   };
  14.  
  15.   fetch('http://localhost:8090', {
  16.     method: 'POST',
  17.     headers: {
  18.       'Content-Type': 'application/json'
  19.     },
  20.     body: JSON.stringify(data)
  21.   })
  22.   .then(response => {
  23.     if (response.ok) {
  24.       alert('Data created successfully');
  25.     } else {
  26.       throw new Error('Error creating data');
  27.     }
  28.   })
  29.   .catch(error => {
  30.     console.error('Error:', error);
  31.     alert('An error occurred while creating data');
  32.   });
  33. }
  34. </script>
Tags: JavaScript
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement