structured API with controllersroutes
This commit is contained in:
@@ -1,39 +1,40 @@
|
||||
// Get reference to the form
|
||||
const form = document.querySelector("form");
|
||||
window.addEventListener("load", function () {
|
||||
const form = document.querySelector("form");
|
||||
|
||||
// Function to handle form submission
|
||||
async function handleSubmit(event) {
|
||||
event.preventDefault();
|
||||
// Function to handle form submission
|
||||
async function handleSubmit(event) {
|
||||
event.preventDefault();
|
||||
|
||||
// Get the value entered in the input field
|
||||
const productCode = document.getElementById("productCode").value;
|
||||
try {
|
||||
// Send POST request to server
|
||||
const response = await fetch("http://localhost:3000/submit", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json", // Indicate that the request body is JSON
|
||||
},
|
||||
body: JSON.stringify({ productCode }),
|
||||
});
|
||||
// Get the value entered in the input field
|
||||
const productCode = document.getElementById("productCode").value;
|
||||
try {
|
||||
// Send POST request to server
|
||||
const response = await fetch("http://localhost:3000/api/item-lookup", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json", // Indicate that the request body is JSON
|
||||
},
|
||||
body: JSON.stringify({ productCode }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`Network response was not ok, status: ${response.status}`
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`Network response was not ok, status: ${response.status}`
|
||||
);
|
||||
}
|
||||
|
||||
// Optional: Handle JSON response data here
|
||||
// const data = await response.json();
|
||||
// console.log(data);
|
||||
|
||||
console.log("Data submitted successfully");
|
||||
// Optional: Update UI to reflect success
|
||||
} catch (error) {
|
||||
console.error("Error submitting data:", error);
|
||||
// Optional: Update UI to reflect error
|
||||
}
|
||||
|
||||
// Optional: Handle JSON response data here
|
||||
// const data = await response.json();
|
||||
// console.log(data);
|
||||
|
||||
console.log("Data submitted successfully");
|
||||
// Optional: Update UI to reflect success
|
||||
} catch (error) {
|
||||
console.error("Error submitting data:", error);
|
||||
// Optional: Update UI to reflect error
|
||||
}
|
||||
}
|
||||
|
||||
// Add event listener to the form's submit event
|
||||
form.addEventListener("submit", handleSubmit);
|
||||
// Add event listener to the form's submit event
|
||||
form.addEventListener("submit", handleSubmit);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user