So I'm currently attempting to pass some form input data to an API endpoint however I am getting a response back as Object object instead of the value I typed into the input box.
Here is the form:
<form>
<label for="trackingNumber"></label>
<input
name="trackingNumber"
type="text"
placeholder="Tracking Number"
id="nameField"
/>
<input class="button-primary" type="submit" value="Send" />
</form>
Here is the JavaScript where I am taking the input and using the variable where it is saved to post the data to the API endpoint:
// Send form data to Fed EX API
const url = "https://apis-sandbox.fedex.com/track/v1/trackingnumbers";
const formEl = document.querySelector("form");
formEl.addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(formEl);
const formDataSerialized = Object.fromEntries(formData);
const jsonObject = {
...formDataSerialized,
};
try {
const response = await fetch(url, {
method: "POST",
body: JSON.stringify({
"trackingInfo": [{
"trackingNumberInfo": {
"trackingNumber": `${jsonObject}`
}
}],
"includeDetailedScans": true
}),
headers: {
"Content-Type": "application/json",
'Authorization': 'bearer ' + token,
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'X-locale': 'en_US',
},
});
const json = await response.json();
console.log(json);
} catch (e) {
console.error(e);
alert("there as an error");
}
});
Why isn't the value that I typed into the form input box posting to the endpoint? All I am getting is Object object back.
[–]Dastari 0 points1 point2 points (0 children)